MCP Serverless

MCP Serverless

site icon
2025.03.10 2
TypeScript工具管理无服务器架构开发效率
MCP Serverless 是一个基于 Model Context Protocol (MCP) 架构的无服务器实现,提供工具管理的简洁接口。它允许用户注册和管理工具,处理工具相关请求,创建内存中的客户端-服务器连接,并通过上下文扩展请求以支持凭证传输。
View on GitHub

Overview

基本能力

产品定位

MCP Serverless 是一个无服务器工具管理平台,专注于提供工具注册、管理和调用的能力。

核心功能

  • 注册和管理工具
  • 处理工具相关请求
  • 创建内存中的客户端-服务器连接
  • 通过上下文扩展请求以支持凭证传输

适用场景

  • 开发工具库的管理和调用
  • 无服务器架构中的工具集成
  • 需要动态扩展工具的场景

工具列表

  • ToolManager:管理工具的注册和处理
  • createService:创建内存中的客户端-服务器设置
  • StdioClientTransportStdioServerTransport:通过标准输入/输出进行通信的传输工具

常见问题解答

  • 如何注册工具?通过 ToolManagerregisterTools 方法注册工具。
  • 如何调用工具?使用 client.callTool 方法调用工具。
  • 如何传输凭证?通过 ctx 参数传递凭证。

使用教程

使用依赖

安装 Node.js 和 npm。

安装教程

npm install @tilfin/mcp-serverless

调试方式

  1. 注册工具并创建客户端
import { createService, ToolManager } from '@tilfin/mcp-serverless';

const toolManager = new ToolManager();
toolManager.registerTools([
  {
    name: 'calculator',
    description: 'Performs basic arithmetic operations',
    inputSchema: {
      type: 'object',
      properties: {
        operation: { type: 'string' },
        numbers: { type: 'array', items: { type: 'number' } }
      },
      required: ['operation', 'numbers']
    },
    toolFunction: async (params, ctx) => {
      if (ctx.apiKey !== 'xyz') throw new Error('Invalid API Key');

      let result;
      if (params.operation === 'add') {
        result = params.numbers.reduce((sum, n) => sum + n, 0);
      }
      return { result };
    }
  }
]);

const client = createService(toolManager);
  1. 调用工具
const toolsList = await client.listTools();

try {
  const result = await client.callTool({
    name: 'calculator',
    arguments: {
      operation: 'add',
      numbers: [1, 2, 3]
    }
  });
} catch (err) {
  // handle error
}
  1. 调用工具并传输凭证
const result = await client.callTool({
  name: 'calculator',
  arguments: {
    operation: 'add',
    numbers: [1, 2, 3]
  },
  ctx: { apiKey: 'xyz' }
});

许可证

该项目遵循 MIT 开源许可条款,请参阅 MIT 了解完整条款。