MCP服务器实现

一个使用TypeScript SDK和Bun运行时的基本模型上下文协议(MCP)服务器实现,支持工具注册、类型安全和快速执行。

  • by gedankrayze
  • TypeScript
  • 本地部署
  • 更新于
在 GitHub 查看 gedankrayze/ts-demo-mcp

使用说明

TypeScript MCP演示服务器

使用TypeScript SDK和Bun运行时的基本模型上下文协议(MCP)服务器实现。

概述

本项目演示了如何使用TypeScript构建MCP服务器,其特点是:

  • 使用stdio传输进行服务器初始化
  • 工具登记和处理
  • TypeScript类型安全
  • Bun运行时实现快速执行

先决条件

  • 包子 (最新版本)
  • 任务 (可选,用于任务运行器)

安装

# Clone the repository
git clone https://github.com/gedankrayze/ts-demo-mcp.git
cd ts-demo-mcp

# Install dependencies
bun install

项目结构

ts-demo-mcp/
├── src/
│   └── index.ts        # Main MCP server implementation
├── package.json        # Project dependencies and scripts
├── tsconfig.json       # TypeScript configuration
├── Taskfile.yaml       # Task runner configuration
└── README.md          # This file

可用工具

服务器实现了两个示例工具:

1. greet

用用户的名字问候他们。

输入架构:

{
  "name": "string"  // Required: The name of the person to greet
}

示例响应:

Hello, John! Welcome to the TypeScript MCP demo server.

2. add_numbers

将两个数字相加。

输入架构:

{
  "a": "number",  // Required: The first number
  "b": "number"   // Required: The second number
}

示例响应:

The sum of 5 and 3 is 8.

用法

使用npm/bun脚本

# Start the MCP server
bun start

# Run TypeScript type checking
bun typecheck

使用任务运行器

# Start the MCP server
task start

# Inspect server capabilities with MCP inspector
task inspect-server

# Send a raw initialization request
task inspect

与Claude Desktop集成

要将此服务器与Claude Desktop一起使用,请将以下内容添加到您的Claude Desktop配置中:

{
  "mcpServers": {
    "ts-demo-mcp": {
      "command": "bun",
      "args": ["run", "/path/to/ts-demo-mcp/src/index.ts"]
    }
  }
}

发展

添加新工具

要向服务器添加新工具,请执行以下操作:

  1. 更新中的工具列表 ListToolsRequestSchema 处理器
  2. 在中添加工具实现 CallToolRequestSchema 处理器

例子:

// In ListToolsRequestSchema handler
{
  name: "my_tool",
  description: "Description of what the tool does",
  inputSchema: {
    type: "object",
    properties: {
      param1: { type: "string", description: "Parameter description" }
    },
    required: ["param1"]
  }
}

// In CallToolRequestSchema handler
case "my_tool": {
  const param1 = request.params.arguments?.param1 as string;
  return {
    content: [{
      type: "text",
      text: `Tool output for ${param1}`
    }]
  };
}

类型检查

该项目使用启用严格模式的TypeScript。运行类型检查:

bun typecheck

MCP检查员

该项目包括用于调试的MCP检查员。使用它来探索服务器功能:

task inspect-server

这将启动一个交互式web界面来测试您的MCP服务器。

许可证

该项目已公开,可在 https://github.com/gedankrayze/ts-demo-mcp.

资源