MCP 服务器样板
使用 TypeScript 和 Express 构建的模型上下文协议 (MCP) 样板服务器实现。
目录
Related MCP server: MCP Server Example
概述
该项目实现了一个遵循模型上下文协议 (MCP) 的服务器,允许应用程序以标准化的方式为 LLM 提供上下文。它包括:
一个完全配置的 MCP 服务器,具有 HTTP 和 stdio 传输选项
用于演示关键功能的示例资源、工具和提示
TypeScript 支持类型安全并改善开发人员体验
HTTP 传输层的快速集成
项目结构
mcp-server-boilerplate/
├── .env # Environment variables
├── .env.example # Example environment variables
├── .gitignore # Git ignore file
├── package.json # Project dependencies and scripts
├── tsconfig.json # TypeScript configuration
├── src/
│ ├── index.ts # Main HTTP server entry point
│ ├── stdio.ts # Stdio server entry point
│ ├── resources/ # MCP resources
│ │ ├── index.ts # Resource registration
│ │ ├── infoResource.ts # Static info resource
│ │ └── greetingResource.ts # Dynamic greeting resource
│ ├── tools/ # MCP tools
│ │ ├── index.ts # Tool registration
│ │ ├── calculatorTool.ts # Sample calculator tool
│ │ └── timestampTool.ts # Sample timestamp tool
│ └── prompts/ # MCP prompts
│ ├── index.ts # Prompt registration
│ ├── greetingPrompt.ts # Sample greeting prompt
│ └── analyzeDataPrompt.ts # Sample data analysis prompt
└── dist/ # Compiled JavaScript output入门
先决条件
Node.js(v18 或更高版本)
npm 或 yarn
安装
克隆存储库并安装依赖项:
git clone https://github.com/yourusername/mcp-server-boilerplate.git
cd mcp-server-boilerplate
npm install环境变量
复制示例环境文件并根据需要修改:
cp .env.example .env可用的环境变量:
PORT:HTTP 服务器的端口(默认值:3000)NODE_ENV:环境模式(开发、生产)OAuth 设置(如果需要)
运行服务器
HTTP 服务器
构建并启动 HTTP 服务器:
npm run build
npm start对于自动重启的开发:
npm run dev该服务器将在http://localhost:3000/mcp (或 .env 文件中指定的端口)上可用。
标准输入输出模式
要在 stdio 模式下运行服务器(对于命令行工具):
npm run start:stdio对于自动重启的开发:
npm run dev:stdio资源
样板包括以下示例资源:
静态信息资源:
info://server提供有关服务器的基本信息
动态问候资源:
greeting://{name}使用提供的名称参数生成个性化问候语
要访问资源:
通过MCP协议
使用 MCP 客户端库
工具
样板包括以下示例工具:
计算器:执行基本的算术运算
参数:
operation:要执行的操作(加、减、乘、除)a:第一个数字b:第二个数字
时间戳:以各种格式提供当前时间
参数:
format:输出格式(iso,unix,可读)
提示
样板包括以下示例提示:
问候语:创建个性化的问候提示
参数:
name:问候的名字formal:是否使用正式的问候风格(可选)
分析数据:创建数据分析提示
参数:
data:要分析的数据format:数据格式(json、csv、text)instructions:附加分析说明(可选)
扩展服务器
添加资源
要添加新资源:
在
src/resources/中创建一个新文件(例如myResource.ts)实现资源处理程序
在
src/resources/index.ts中注册
例子:
// myResource.ts
import { McpServer } from '@modelcontextprotocol/sdk/server/mcp.js';
export function myResource(server: McpServer): void {
server.resource('my-resource', 'my-resource://path', async uri => ({
contents: [
{
uri: uri.href,
text: 'My resource content',
},
],
}));
}
// Then add to resources/index.ts
import { myResource } from './myResource.js';
export function registerResources(server: McpServer): void {
// ...existing resources
myResource(server);
}添加工具
要添加新工具:
在
src/tools/中创建一个新文件(例如,myTool.ts)实现工具处理程序
在
src/tools/index.ts中注册
例子:
// myTool.ts
import { McpServer } from '@modelcontextprotocol/sdk/server/mcp.js';
import { z } from 'zod';
export function myTool(server: McpServer): void {
server.tool('my-tool', { param: z.string() }, async ({ param }) => ({
content: [
{
type: 'text',
text: `Processed: ${param}`,
},
],
}));
}
// Then add to tools/index.ts
import { myTool } from './myTool.js';
export function registerTools(server: McpServer): void {
// ...existing tools
myTool(server);
}添加提示
要添加新提示:
在
src/prompts/中创建一个新文件(例如myPrompt.ts)实现提示处理程序
在
src/prompts/index.ts中注册
例子:
// myPrompt.ts
import { McpServer } from '@modelcontextprotocol/sdk/server/mcp.js';
import { z } from 'zod';
export function myPrompt(server: McpServer): void {
server.prompt('my-prompt', { topic: z.string() }, ({ topic }) => ({
messages: [
{
role: 'user',
content: {
type: 'text',
text: `Please explain ${topic} in simple terms.`,
},
},
],
}));
}
// Then add to prompts/index.ts
import { myPrompt } from './myPrompt.js';
export function registerPrompts(server: McpServer): void {
// ...existing prompts
myPrompt(server);
}测试和调试
要测试您的 MCP 服务器,您可以使用:
MCP 检查器工具
MCP 客户端库
直接 HTTP 请求(用于调试)
执照
该项目根据 MIT 许可证获得许可 - 有关详细信息,请参阅 LICENSE 文件。