Back to MCP directory
publicPublicdnsLocal runtime

mcp-framework

MCP-Framework是一个基于TypeScript的框架,用于优雅地构建模型上下文协议(MCP)服务器。它提供开箱即用的架构,支持自动目录发现工具、资源和提示,具有多种传输方式和类型安全特性。

article

README

🚀 MCP 框架指南

MCP 框架是一款功能强大的工具,可极大简化实时通信应用的开发工作。它支持 SSE 和 HTTP 流等多种传输协议,还配备了认证、日志记录和错误处理等丰富的功能模块。

🚀 快速开始

📦 安装依赖

npm install mcp-framework

💻 使用示例

基础用法

import { MCPServer } from 'mcp-framework';

const server = new MCPServer({
  transport: {
    type: 'sae',
    options: {
      port: 3000,
      host: 'localhost'
    }
  }
});

server.start();

运行示例

node index.js

✨ 主要特性

MCP 框架通过中间件机制实现模块化功能。用户能够依据自身需求挑选不同的中间件,进而扩展服务功能。

📚 详细文档

SSE 传输配置

SSE(Server-Sent Events)是一种用于实时通信的协议,适用于需要持续数据流的应用场景。

基本配置

import { MCPServer } from 'mcp-framework';

const server = new MCPServer({
  transport: {
    type: 'sae',
    options: {
      port: 3000,
      host: 'localhost'
    }
  }
});

server.start();

高级配置

import { MCPServer, SSETransportOptions } from 'mcp-framework';

const server = new MCPServer({
  transport: {
    type: 'sae',
    options: {
      port: 3000,
      host: 'localhost',
      path: '/events',
      maxReconnectionDelay: 5000
    }
  }
});

server.start();

HTTP 流传输配置

HTTP 流传输适用于需要处理大文件上传和下载的场景。

基本配置

import { MCPServer } from 'mcp-framework';

const server = new MCPServer({
  transport: {
    type: 'http-stream',
    options: {
      port: 3000,
      host: 'localhost'
    }
  }
});

server.start();

高级配置

import { MCPServer, HTTPStreamOptions } from 'mcp-framework';

const server = new MCPServer({
  transport: {
    type: 'http-stream',
    options: {
      port: 3000,
      host: 'localhost',
      path: '/stream',
      responseMode: 'batch',
      batchTimeout: 30000
    }
  }
});

server.start();

认证

MCP 框架支持多种认证方式,包括 JWT 和 API Key。

JWT 认证配置

import { MCPServer, JWTAuthProvider } from 'mcp-framework';

const server = new MCPServer({
  transport: {
    type: 'sae',
    options: {
      auth: {
        provider: new JWTAuthProvider({
          secret: process.env.JWT_SECRET,
          algorithms: ['HS256']
        }),
        endpoints: {
          sse: true
        }
      }
    }
  }
});

server.start();

API Key 认证配置

import { MCPServer, APIKeyAuthProvider } from 'mcp-framework';

const server = new MCPServer({
  transport: {
    type: 'sae',
    options: {
      auth: {
        provider: new APIKeyAuthProvider({
          secret: process.env.API_KEY_SECRET
        })
      }
    }
  }
});

server.start();

日志记录

MCP 框架提供内置日志记录功能,帮助开发者调试和监控服务。

import { MCPServer, Logger } from 'mcp-framework';

const logger = new Logger({
  level: 'info',
  handlers: [
    {
      type: 'file',
      path: './logs/app.log'
    }
  ]
});

const server = new MCPServer({
  transport: {
    type: 'sae',
    options: {
      port: 3000,
      host: 'localhost'
    }
  },
  logger
});

server.start();

错误处理

MCP 框架提供强大的错误处理机制,帮助开发者快速定位和解决问题。

import { MCPServer } from 'mcp-framework';

const server = new MCPServer({
  transport: {
    type: 'sae',
    options: {
      port: 3000,
      host: 'localhost'
    }
  },
  errorHandlers: [
    (error, context) => {
      console.error('Custom error handler:', error);
      context.reply(500, 'Internal Server Error');
    }
  ]
});

server.start();

🔚 总结

MCP 框架通过提供灵活的配置和强大的功能模块,简化了实时通信应用的开发过程。无论是 SSE 还是 HTTP 流传输,用户都可以根据需求选择合适的方案,并通过丰富的中间件扩展服务功能。

help

Runtime guide

cloud

Hosted runtime

Hosted servers run from a provider-managed environment. You usually connect the MCP client to the hosted endpoint or follow the provider's authorization flow, without keeping a local process alive

  1. Open provider connection page
  2. Authorize or copy endpoint
  3. Connect from your MCP client
terminal

Local runtime / other methods

Local servers run on your own machine or infrastructure. You normally copy the server_config into your MCP client, install the required package, and provide env variables from env_schema when needed

  1. Copy server_config
  2. Install required package
  3. Fill env variables and restart client