使用说明
MCP服务器
模型上下文协议(MCP)的稳健服务器实现,支持多种传输方法(SSE和STDIO),用于与AI模型进行实时通信。
🚀 特性
- 完整的MCP实施:完整且符合标准的模型上下文协议实施
- 多个传输:通过SSE(服务器发送事件)和STDIO实现无缝通信
- 稳健的体系结构:基于清洁架构和SOLID原则,可维护性和可扩展性
- 强大的工具:易于扩展的工具、资源和提示
- 开发者友好:全面的日志记录、错误处理和测试支持
🏗️ 建筑
该项目遵循遵循SOLID原则的干净、分层的架构:
| 层 | 责任 | 组件 |
|---|---|---|
| 领域 | 核心业务逻辑 | 接口、模型、实体 |
| 应用 | 用例、应用程序流 | 服务、用例、DTO |
| 基础设施 | 技术问题 | 实施、适配器、外部服务 |
| 演示 | 用户界面 | API控制器,HTTP端点 |
建筑图
┌─────────────────────┐ ┌─────────────────────┐
│ Presentation │ │ Application │
│ Layer │ │ Layer │
│ - Express API │─────▶│ - Use Cases │
│ - Controllers │ │ - Services │
└─────────────────────┘ └──────────┬──────────┘
│
▼
┌─────────────────────┐ ┌─────────────────────┐
│ Infrastructure │ │ Domain │
│ Layer │◀─────│ Layer │
│ - Implementations │ │ - Interfaces │
│ - Adapters │ │ - Models │
└─────────────────────┘ └─────────────────────┘
💻 需求
- Node.js 18或更高版本
- Yarn或npm包管理器
📦 安装
# Clone the repository
git clone https://github.com/your-username/mcp-server.git
cd mcp-server
# Install dependencies
yarn install
# Build the project
yarn build
🚀 运行服务器
# Start the server
yarn start
# Build and start in one command (for development)
yarn rebuild
默认情况下,服务器在端口3001上运行。您可以使用配置 PORT 环境变量。
🔌 运输选项
MCP服务器支持两种传输方式:
| 运输 | 描述 | 用例 |
|---|---|---|
| 上海证券交易所 | 服务器通过HTTP发送事件 | Web应用程序、浏览器客户端 |
| 工作室 | 标准输入/输出流 | CLI工具、本地应用程序 |
🛣️ API终点
- 上海证券交易所:
/sse-建立SSE连接以进行实时通信 - 消息:
/messages-向服务器发送JSON-RPC消息
🧪 测试
使用官方的MCP Inspector工具测试您的服务器实现:
- 构建并启动MCP服务器
yarn build
yarn rebuild # if using SSE transport (builds & starts)
# OR
yarn start # manual start
- 对服务器运行检查器
npx @modelcontextprotocol/inspector node build/index.js
- 访问Inspecto UI http://127.0.0.1:6274 🚀
🧩 扩展功能
添加新工具
// tools/custom/my-tool.ts
import { z } from "zod";
import { McpServer } from "@modelcontextprotocol/sdk/server/mcp.js";
import { ITool } from "../core/domain/interfaces/tool.interface.js";
import { ILogger } from "../core/domain/interfaces/logger.interface.js";
export class MyTool implements ITool {
private readonly logger: ILogger;
constructor(logger: ILogger) {
this.logger = logger;
}
public register(server: McpServer): void {
server.tool(
"my-tool",
"Description of my tool",
{
param1: z.string().describe("Parameter description"),
},
async ({ param1 }) => {
this.logger.info(`MyTool called with param1: ${param1}`);
return {
content: [
{
type: "text",
text: `Result: ${param1}`,
},
],
};
},
);
this.logger.info("MyTool registered");
}
}
添加新资源
// resources/custom/my-resource.ts
import { McpServer } from "@modelcontextprotocol/sdk/server/mcp.js";
import { IResource } from "../core/domain/interfaces/resource.interface.js";
import { ILogger } from "../core/domain/interfaces/logger.interface.js";
export class MyResource implements IResource {
private readonly logger: ILogger;
constructor(logger: ILogger) {
this.logger = logger;
}
public register(server: McpServer): void {
server.resource('my-resource', 'my-data.json', async () => {
this.logger.info('My resource accessed');
return {
contents: [
{
uri: 'my-data.json',
text: JSON.stringify({ key: "value" }, null, 2),
mimeType: 'application/json',
},
],
};
});
this.logger.info('My resource registered');
}
}
添加新提示
// prompts/custom/my-prompt.ts
import { McpServer } from "@modelcontextprotocol/sdk/server/mcp.js";
import { IPrompt } from "../core/domain/interfaces/prompt.interface.js";
import { ILogger } from "../core/domain/interfaces/logger.interface.js";
export class MyPrompt implements IPrompt {
private readonly logger: ILogger;
constructor(logger: ILogger) {
this.logger = logger;
}
public register(server: McpServer): void {
server.prompt(
'my-prompt',
'Description of my prompt',
() => {
this.logger.info('My prompt accessed');
return {
messages: [
{
role: 'user',
content: {
type: 'text',
text: 'Initial user message',
},
},
{
role: 'assistant',
content: {
type: 'text',
text: 'Initial assistant response',
},
},
],
};
},
);
this.logger.info('My prompt registered');
}
}
📝 应用固体原理
| 原理 | 实施 |
|---|---|
| 单一责任 | 每个类都有一个明确的职责 |
| 打开/关闭 | 无需修改现有代码即可扩展系统 |
| 利斯科夫替补 | 派生类型可以替换其基类型 |
| 接口隔离 | 特定客户端的特定接口 |
| 依赖倒置 | 高级模块依赖于抽象 |
📄 许可证
此项目根据MIT许可证获得许可-有关详细信息,请参阅许可证文件。