模型上下文协议服务器模板

一个基于TypeScript和Express的Model Context Protocol (MCP)服务器模板,用于为LLMs提供标准化的上下文。

  • by cds-id
  • TypeScript
  • 本地部署
  • 1 星标
  • 更新于
在 GitHub 查看 cds-id/mcp-server-boilerplate

使用说明

MCP服务器主板

模型上下文协议(MCP)的样板服务器实现,使用TypeScript和Express构建。

目录

概述

该项目实现了一个遵循模型上下文协议(MCP)的服务器,该协议允许应用程序以标准化的方式为LLM提供上下文。它包括:

  • 具有HTTP和stdio传输选项的完全配置的MCP服务器
  • 演示关键功能的示例资源、工具和提示
  • 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或纱线

安装

克隆存储库并安装依赖项:

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

资源

样板包括以下示例资源:

  1. 静态信息资源: info://server

    • 提供有关服务器的基本信息
  2. 动态问候资源: greeting://{name}

    • 使用提供的name参数生成个性化问候语

要访问资源,请执行以下操作:

  • 通过MCP协议
  • 使用MCP客户端库

工具

样板包括以下示例工具:

  1. 计算器:执行基本算术运算

    • 参数:
      • operation:要执行的操作(加、减、乘、除)
      • a:第一个数字
      • b:第二个数字
  2. 时间戳:以各种格式提供当前时间

    • 参数:
      • format:输出格式(iso、unix、可读)

提示词

样板包括以下示例提示:

  1. 问候:创建个性化问候提示

    • 参数:
      • name:要问候的名字
      • formal:是否使用正式问候风格(可选)
  2. 分析数据:创建数据分析提示

    • 参数:
      • data:要分析的数据
      • format:数据格式(json、csv、文本)
      • instructions:附加分析说明(可选)

扩展服务器

添加资源

要添加新资源,请执行以下操作:

  1. 在中创建新文件 src/resources/ (例如。, myResource.ts)
  2. 实现您的资源处理程序
  3. 在中注册 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);
}

添加工具

要添加新工具,请执行以下操作:

  1. 在中创建新文件 src/tools/ (例如。, myTool.ts)
  2. 实施您的工具处理程序
  3. 在中注册 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);
}

添加提示

要添加新提示,请执行以下操作:

  1. 在中创建新文件 src/prompts/ (例如。, myPrompt.ts)
  2. 实现你的提示处理程序
  3. 在中注册 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许可证获得许可-有关详细信息,请参阅许可证文件。