MCP服务器实现

一个基于JSON-RPC 2.0协议的MCP服务器实现,提供HTTP接口和电话簿API功能,适用于需要简单API服务的开发场景。

  • by fritogotlayed
  • TypeScript
  • 本地部署
  • 更新于
在 GitHub 查看 fritogotlayed/sample-mcp-server

使用说明

MCP服务器示例

支持HTTP的机器对话协议(MCP)服务器的示例实现。此服务器提供了一个简单的 可通过HTTP访问的电话簿API。

特性

  • 遵循JSON-RPC 2.0协议的MCP服务器实现
  • API访问的HTTP接口
  • 具有CRUD操作的简单电话簿API
  • 基于工具的可扩展性架构

安装

  1. 确保你有 德诺 安装
  2. 克隆此存储库
  3. 使用提供的任务之一运行服务器
# Install Deno (if not already installed)
curl -fsSL https://deno.land/x/install/install.sh | sh

# Clone the repository (example)
git clone https://github.com/yourusername/sample-mcp-server.git
cd sample-mcp-server

# Run the server
deno task start

用法

服务器模式

服务器以HTTP模式运行,在指定端口上侦听HTTP请求。

启动服务器

您可以使用中提供的任务启动服务器 deno.json:

# Start the server
deno task start

或者,您可以直接使用命令行参数运行main.ts文件:

# Start the server
deno run --allow-read --allow-write --allow-net main.ts

# Specify a custom port (default is 8000)
deno run --allow-read --allow-write --allow-net main.ts --port=3000

发展

对于具有文件监视功能的开发:

deno task dev

测试

运行测试:

deno task test

具体测试HTTP服务器:

# Start the server in HTTP mode
deno task start:http

# In another terminal, run the HTTP test script
deno run --allow-net test-http-server.ts

API

服务器使用JSON-RPC 2.0实现机器对话协议(MCP)。它支持以下方法:

核心方法

  • initialize:初始化MCP会话
  • tools/list:列出可用工具
  • tools/call:调用特定工具

可用工具

  • list_persons:列出电话簿中的所有人
  • get_person:通过ID获取特定人员
  • add_person:将新用户添加到电话簿
  • update_person:更新现有人员
  • delete_person:从电话簿中删除某人
  • search_persons:按姓名、电子邮件或电话搜索人员

HTTP API示例

初始化

curl -X POST http://localhost:8000 \
  -H "Content-Type: application/json" \
  -d '{
    "jsonrpc": "2.0",
    "id": 1,
    "method": "initialize"
  }'

列出工具

curl -X POST http://localhost:8000 \
  -H "Content-Type: application/json" \
  -d '{
    "jsonrpc": "2.0",
    "id": 2,
    "method": "tools/list"
  }'

调用工具(添加人员)

curl -X POST http://localhost:8000 \
  -H "Content-Type: application/json" \
  -d '{
    "jsonrpc": "2.0",
    "id": 3,
    "method": "tools/call",
    "params": {
      "name": "add_person",
      "arguments": {
        "name": "John Doe",
        "email": "john@example.com",
        "phone": "123-456-7890"
      }
    }
  }'

项目结构

  • main.ts:主要入口点
  • deno.json:项目配置
  • database.json:实现为JSON的示例数据库文件
  • src/models/types.ts:域模型的类型定义
  • src/services/database.service.ts:数据库服务
  • src/services/tools.service.ts:工具服务
  • src/tools/:工具实施

许可证

MIT许可证