Skip to main content
Glama
cds-id

MCP Server Boilerplate

by cds-id

MCP 服务器样板

MCP TypeScript SDK NPM 版本 MIT 许可证

使用 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

资源

样板包括以下示例资源:

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

    • 提供有关服务器的基本信息

  2. 动态问候资源greeting://{name}

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

要访问资源:

  • 通过MCP协议

  • 使用 MCP 客户端库

工具

样板包括以下示例工具:

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

    • 参数:

      • operation :要执行的操作(加、减、乘、除)

      • a :第一个数字

      • b :第二个数字

  2. 时间戳:以各种格式提供当前时间

    • 参数:

      • format :输出格式(iso,unix,可读)

提示

样板包括以下示例提示:

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

    • 参数:

      • name :问候的名字

      • formal :是否使用正式的问候风格(可选)

  2. 分析数据:创建数据分析提示

    • 参数:

      • data :要分析的数据

      • format :数据格式(json、csv、text)

      • 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 许可证获得许可 - 有关详细信息,请参阅 LICENSE 文件。

Install Server
A
security – no known vulnerabilities
F
license - not found
A
quality - confirmed to work

Latest Blog Posts

MCP directory API

We provide all the information about MCP servers via our MCP API.

curl -X GET 'https://glama.ai/api/mcp/v1/servers/cds-id/mcp-server-boilerplate'

If you have feedback or need assistance with the MCP directory API, please join our Discord server