远程模型上下文协议

一个类型安全、双向且简单的远程MCP通信解决方案,用于远程访问和集中管理模型上下文。

  • by ssut
  • TypeScript
  • 本地部署
  • 207 星标
  • 更新于
在 GitHub 查看 ssut/Remote-MCP

使用说明

远程MCP:远程模型上下文协议

A. 类型安全、双向、简单 解决方案 远程MCP通信,允许远程访问和集中管理模型上下文。

建筑

%%{init: {"flowchart": {"htmlLabels": false}} }%%
graph TD
    %% Modern, Bright Color Styling with white text
    classDef client fill:#22c55e,stroke:#059669,stroke-width:2px,color:#ffffff
    classDef gateway fill:#06b6d4,stroke:#0891b2,stroke-width:2px,color:#ffffff
    classDef backend fill:#f97316,stroke:#ea580c,stroke-width:2px,color:#ffffff
    classDef resource fill:#8b5cf6,stroke:#7c3aed,stroke-width:2px,color:#ffffff
    classDef server fill:#06b6d4,stroke:#0891b2,stroke-width:2px,color:#ffffff

    linkStyle default stroke:#64748b,stroke-width:1.5px,stroke-dasharray: 5 5

    %% Current MCP Setup (Multiple Local Servers)
    subgraph Current["Current Setup (Local)"]
        direction LR
        subgraph ClientGroup["Client"]
            A[Client]:::client
        end

        subgraph Servers["Local MCP Servers"]
            direction TB
            B1["Local MCP Server (DB)"]:::server -->|"DB Access"| C1[DB]:::resource
            B2["Local MCP Server (API 1)"]:::server -->|"API Access"| C2["Web API 1"]:::resource
            B3["Local MCP Server (API 2)"]:::server -->|"API Access"| C3["Web API 2"]:::resource
        end

        A -->|"MCP Protocol"| B1
        A -->|"MCP Protocol"| B2
        A -->|"MCP Protocol"| B3
    end

    %% Vertical separator
    Current --> Proposed

    %% Proposed MCP Architecture (Decoupled)
    subgraph Proposed["Proposed Architecture (Remote)"]
        direction LR
        D[Client/Host]:::client -->|"MCP Protocol"| E["Local MCP Server (@remote-mcp/client)"]:::server
        E <-->|"tRPC(HTTP)"| F["Remote MCP Server (@remote-mcp/server)"]:::backend

        %% Separated Resources
        F -->|"DB Access"| G1[DB]:::resource
        F -->|"API Access"| G2["Web API 1"]:::resource
        F -->|"API Access"| G3["Web API 2"]:::resource
    end

我为什么做这个(现在)

是的,我知道官方的MCP路线图包括2025年第一季度的远程MCP支持。然而,远程访问的需求 立即 对我来说,也可能对许多其他人来说。创建此库是为了弥合这一差距,提供了一种从本地MCP客户端连接到远程MCP服务器的方法 马上,无需等待未来的官方实施。

注意:我不希望这是一件复杂或过于复杂的事情。这边 现在就可以了.

入门指南

注:该项目目前正在积极开发中,被认为是实验性的。期待突破性的变化和潜在的问题。

客户端使用情况

使用公开发布的软件包

只需将以下代码放入MCP客户端设置中,这里我使用Claude作为示例:

{
  "mcpServers": {
    "remote-mcp": {
      "command": "npx",
      "args": ["-y", "@remote-mcp/client"],
      "env": {
        "REMOTE_MCP_URL": "http://localhost:9512",
        "HTTP_HEADER_Authorization": "Bearer <token>"
      }
    }
  }
}

编写您自己的本地MCP服务器代码

安装要求:

$ npm install @remote-mcp/client @trpc/client@next zod

然后编写自己的代码,如下所示:

import { RemoteMCPClient } from "@remote-mcp/client";

const client = new RemoteMCPClient({
  remoteUrl: "http://localhost:9512",

  onError: (method, error) => console.error(`Error in ${method}:`, error)
});

void client.start();

服务器使用情况(远程MCP实施)

您可以在 examples 目录。

编写您自己的远程MCP服务器代码

之后 npm install @remote-mcp/server,您可以使用自己的远程MCP服务器,如下所示:

import { MCPRouter, LogLevel } from "@remote-mcp/server";
import { createHTTPServer } from '@trpc/server/adapters/standalone';

import { z } from "zod";

// Create router instance
const mcpRouter = new MCPRouter({
  logLevel: LogLevel.DEBUG,
  name: "example-server",
  version: "1.0.0",
  capabilities: {
    logging: {},
  },
});

// Add example tool
mcpRouter.addTool(
  "calculator",
  {
    description:
      "Perform basic calculations. Add, subtract, multiply, divide. Invoke this every time you need to perform a calculation.",
    schema: z.object({
      operation: z.enum(["add", "subtract", "multiply", "divide"]),
      a: z.string(),
      b: z.string(),
    }),
  },
  async (args) => {
    const a = Number(args.a);
    const b = Number(args.b);

    let result: number;
    switch (args.operation) {
      case "add":
        result = Number(a) + b;
        break;
      case "subtract":
        result = a - b;
        break;
      case "multiply":
        result = a * b;
        break;
      case "divide":
        if (b === 0) throw new Error("Division by zero");
        result = a / b;
        break;
    }

    return {
      content: [{ type: "text", text: `${result}` }],
    };
  },
);

const appRouter = mcpRouter.createTRPCRouter();

void createHTTPServer({
  router: appRouter,
  createContext: () => ({}),
}).listen(Number(process.env.PORT || 9512));

然后,您可以在MCP客户端中看到以下内容:

包裹

此存储库包含:

  • @remote-mcp/client:客户端库充当本地MCP服务器,连接到远程实现。
  • @remote-mcp/server:用于创建可远程访问的MCP服务的服务器库(用作远程实现)。

路线图

核心功能

  • [x] 基础 类型安全 客户端/服务器通信
    • [x] 基本MCP命令支持
    • [x] 基本MCP工具支持
    • [x] 基本MCP提示支持
    • []碰撞安全处理(WIP,首要任务)
  • []完整的活动订阅系统
    • []资源更改通知
    • []工具/提示列表更改通知
  • []HTTP标头支持
    • [x] 自定义标题
    • []身份验证中间件
  • []基本错误处理改进
  • []基本中间件支持

框架支持

  • []Nest.js集成(@remote-mcp/nestjs)

高级功能

  • []双向通信
    • []服务器到客户端请求
    • []服务器/客户端之间的资源共享
  • []基本监测和记录

贡献

欢迎捐款。看 贡献.md 了解详情。

免责声明

该库是一个补充扩展,不是官方MCP规范的一部分,建立在现有的MCP概念之上。

许可证

该项目根据MIT许可证获得许可。请参阅 许可证 文件以获取详细信息。

参考文献