Skip to main content
Glama

MCP框架

用于构建模型上下文协议 (MCP) 服务器的 TypeScript 框架。

与原版的差异

该分支(@ronangrant/mcp-framework)包括以下改进:

  • 用仅控制台的日志记录替换基于文件的日志记录,以获得更好的兼容性和可靠性

  • 消除了对文件系统日志的依赖,消除了 ENOENT 错误

  • 简化日志记录实现,同时保持相同的接口

  • 所有日志现在通过 console.error() 输出到 stderr

Related MCP server: MCP Server Starter

安装

npm install @ronangrant/mcp-framework

用法

创建一个新的 MCP 服务器:

import { MCPServer } from '@ronangrant/mcp-framework'; const server = new MCPServer({ name: "my-server", version: "1.0.0" }); await server.start();

特征

  • 用于创建 MCP 服务器的易于使用的 API

  • 内置对工具、提示和资源的支持

  • 带有控制台输出的简化日志系统

  • 全面支持 TypeScript

  • 灵活的交通选择

执照

麻省理工学院

MCP-Framework 是一个使用 TypeScript 优雅地构建模型上下文协议 (MCP) 服务器的框架。

MCP-Framework 为您提供开箱即用的架构,并支持基于目录的自动工具、资源和提示符发现功能。使用我们强大的 MCP 抽象,您可以优雅地定义工具、资源或提示符。我们的命令行界面让您轻松上手 MCP 服务器。

特征

  • 🛠️ 自动发现和加载工具、资源和提示

  • 多种传输支持(stdio、SSE)

  • 具有完全类型安全性的 TypeScript 优先开发

  • 基于官方 MCP SDK 构建

  • 易于使用的工具、提示和资源基类

  • SSE 端点的开箱即用身份验证

点击此处阅读完整文档

使用 mcp-framework 创建存储库

使用 CLI(推荐)

# Install the framework globally npm install -g mcp-framework # Create a new MCP server project mcp create my-mcp-server # Navigate to your project cd my-mcp-server # Your server is ready to use!

CLI 使用

该框架提供了强大的 CLI 来管理您的 MCP 服务器项目:

项目创建

# Create a new project mcp create <your project name here>

添加工具

# Add a new tool mcp add tool price-fetcher

添加提示

# Add a new prompt mcp add prompt price-analysis

添加资源

# Add a new prompt mcp add resource market-data

开发工作流程

  1. 创建你的项目:

mcp create my-mcp-server cd my-mcp-server
  1. 根据需要添加工具:

    mcp add tool data-fetcher mcp add tool data-processor mcp add tool report-generator
  2. 建造:

    npm run build
  3. 添加到 MCP 客户端(请参阅下面的 Claude Desktop 示例)

与 Claude Desktop 一起使用

本地开发

将此配置添加到您的 Claude Desktop 配置文件:

MacOS :`~/Library/Application Support/Claude/claude_desktop_config.json` Windows :`%APPDATA%/Claude/claude_desktop_config.json`

{ "mcpServers": { "${projectName}": { "command": "node", "args":["/absolute/path/to/${projectName}/dist/index.js"] } } }

发布后

将此配置添加到您的 Claude Desktop 配置文件:

MacOS :`~/Library/Application Support/Claude/claude_desktop_config.json` Windows :`%APPDATA%/Claude/claude_desktop_config.json`

{ "mcpServers": { "${projectName}": { "command": "npx", "args": ["${projectName}"] } } }

构建和测试

  1. 更改你的工具

  2. 运行 `npm run build` 进行编译

  3. 服务器将在启动时自动加载您的工具

快速入门

创建工具

import { MCPTool } from "mcp-framework"; import { z } from "zod"; interface ExampleInput { message: string; } class ExampleTool extends MCPTool<ExampleInput> { name = "example_tool"; description = "An example tool that processes messages"; schema = { message: { type: z.string(), description: "Message to process", }, }; async execute(input: ExampleInput) { return `Processed: ${input.message}`; } } export default ExampleTool;

设置服务器

import { MCPServer } from "mcp-framework"; const server = new MCPServer(); // OR (mutually exclusive!) with SSE transport const server = new MCPServer({ transport: { type: "sse", options: { port: 8080 // Optional (default: 8080) } } }); // Start the server await server.start();

传输配置

stdio 传输(默认)

如果没有提供传输配置,则默认使用 stdio 传输:

const server = new MCPServer(); // or explicitly: const server = new MCPServer({ transport: { type: "stdio" } });

上交所运输

要使用服务器发送事件 (SSE) 传输:

const server = new MCPServer({ transport: { type: "sse", options: { port: 8080, // Optional (default: 8080) endpoint: "/sse", // Optional (default: "/sse") messageEndpoint: "/messages", // Optional (default: "/messages") cors: { allowOrigin: "*", // Optional (default: "*") allowMethods: "GET, POST, OPTIONS", // Optional (default: "GET, POST, OPTIONS") allowHeaders: "Content-Type, Authorization, x-api-key", // Optional (default: "Content-Type, Authorization, x-api-key") exposeHeaders: "Content-Type, Authorization, x-api-key", // Optional (default: "Content-Type, Authorization, x-api-key") maxAge: "86400" // Optional (default: "86400") } } } });

CORS 配置

SSE 传输支持灵活的 CORS 配置。默认情况下,它使用适合开发的宽松设置。对于生产环境,您应该根据自己的安全需求配置 CORS:

const server = new MCPServer({ transport: { type: "sse", options: { // Restrict to specific origin cors: { allowOrigin: "https://myapp.com", allowMethods: "GET, POST", allowHeaders: "Content-Type, Authorization", exposeHeaders: "Content-Type, Authorization", maxAge: "3600" } } } }); // Or with multiple allowed origins const server = new MCPServer({ transport: { type: "sse", options: { cors: { allowOrigin: "https://app1.com, https://app2.com", allowMethods: "GET, POST, OPTIONS", allowHeaders: "Content-Type, Authorization, Custom-Header", exposeHeaders: "Content-Type, Authorization", maxAge: "86400" } } } });

验证

MCP 框架为 SSE 端点提供了可选的身份验证方式。您可以选择 JWT 或 API Key 身份验证,也可以实现自定义的身份验证提供程序。

JWT 身份验证

import { MCPServer, JWTAuthProvider } from "mcp-framework"; import { Algorithm } from "jsonwebtoken"; const server = new MCPServer({ transport: { type: "sse", options: { auth: { provider: new JWTAuthProvider({ secret: process.env.JWT_SECRET, algorithms: ["HS256" as Algorithm], // Optional (default: ["HS256"]) headerName: "Authorization" // Optional (default: "Authorization") }), endpoints: { sse: true, // Protect SSE endpoint (default: false) messages: true // Protect message endpoint (default: true) } } } } });

客户端必须在授权标头中包含有效的 JWT 令牌:

Authorization: Bearer eyJhbGciOiJIUzI1NiIs...

API 密钥认证

import { MCPServer, APIKeyAuthProvider } from "mcp-framework"; const server = new MCPServer({ transport: { type: "sse", options: { auth: { provider: new APIKeyAuthProvider({ keys: [process.env.API_KEY], headerName: "X-API-Key" // Optional (default: "X-API-Key") }) } } } });

客户端必须在 X-API-Key 标头中包含有效的 API 密钥:

X-API-Key: your-api-key

自定义身份验证

您可以通过实现AuthProvider接口来实现自己的身份验证提供程序:

import { AuthProvider, AuthResult } from "mcp-framework"; import { IncomingMessage } from "node:http"; class CustomAuthProvider implements AuthProvider { async authenticate(req: IncomingMessage): Promise<boolean | AuthResult> { // Implement your custom authentication logic return true; } getAuthError() { return { status: 401, message: "Authentication failed" }; } }

执照

麻省理工学院

-
security - not tested
F
license - not found
-
quality - not tested

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/ronangrant/mcp-framework'

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