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"]
}
}
}发展
添加新工具
要向服务器添加新工具,请执行以下操作:
- 更新中的工具列表
ListToolsRequestSchema处理器 - 在中添加工具实现
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 typecheckMCP检查员
该项目包括用于调试的MCP检查员。使用它来探索服务器功能:
task inspect-server这将启动一个交互式web界面来测试您的MCP服务器。
许可证
该项目已公开,可在 .
