# Remote-MCP模型上下文服务
## 基本信息
- Slug: `ssut-remote-mcp`
- Source: modelscope
- Publisher: @ssut/Remote-MCP
- Categories: communication / developer-tools / cloud-platforms
- Hosted: No
- License: MIT License
- Source URL: https://www.modelscope.cn/mcp/servers/@ssut/Remote-MCP
## 简介
启用与模型上下文协议服务的类型安全、双向通信，允许通过 HTTP 集中管理模型上下文。
## 安装提示

```bash
## 我为什么现在做这个 是的，我知道官方MCP路线图计划在2025年第一季度支持远程MCP。然而，对于我以及可能对许多人来说，远程访问的需求是*迫切*的。创建此库是为了填补这一空白，提供一种方法可以立即从本地MCP客户端连接到远程MCP服务器，而无需等待未来的官方实现。 注意：我不希望这变得复杂或过度繁琐。这种方式**现在就能工作**。 ## 开始使用 > *注意：此项目目前正处于积极开发中，被视为实验性质。预计会有破坏性变更及潜在问题。* ## 客户端使用 ### 使用公开发布的包 只需将以下代码放入您的MCP客户端设置中，在这里我以Claude为例：
```

## MCP Server 详情

# Remote-MCP: 远程模型上下文协议

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

![预览](https://github.com/user-attachments/assets/a16804b9-8378-493c-8ca8-f61839458cde)


## 架构

```mermaid
%%{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为例：

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


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

安装需求：

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


然后像下面这样编写您自己的代码：

```ts
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`目录中查看一些示例。

- [Cloudflare Workers](examples/cloudflare-workers)
- [独立Node.js](examples/simple-server)

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

在执行`npm install @remote-mcp/server`后，您可以如下方式编写您自己的远程MCP服务器：

```typescript
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客户端看到类似以下的内容：

<img src="https://github.com/user-attachments/assets/86cf500e-b937-47fc-9ac1-db106ab7a6a3" width="450">

## 包

此仓库包含：

*   `@remote-mcp/client`: 作为本地MCP服务器的客户端库，连接到远程实现。
*   `@remote-mcp/server`: 用于创建可远程访问的MCP服务的服务器库（用作远程实现）。

## 路线图

### 核心功能

- [x] 基本*类型安全*的客户端/服务器通信
  - [x] 基本MCP命令支持
  - [x] 基本MCP工具支持
  - [x] 基本MCP提示支持
  - [ ] 稳健处理 (WIP, 最高优先级)
- [ ] 完整事件订阅系统
  - [ ] 资源变更通知
  - [ ] 工具/提示列表变更通知
- [ ] HTTP头部支持
  - [x] 自定义头部
  - [ ] 认证中间件
- [ ] 基本错误处理改进
- [ ] 基本中间件支持

### 框架支持

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

### 高级功能

- [ ] 双向通信
  - [ ] 服务器到客户端请求
  - [ ] 服务器与客户端之间的资源共享
- [ ] 基本监控与日志记录

## 贡献

欢迎贡献。详情请见[CONTRIBUTING.md](CONTRIBUTING.md)。

## 免责声明

该库是一个补充扩展，不是官方MCP规范的一部分，它基于现有的MCP概念构建。

## 许可证

本项目采用MIT许可证。详情请参阅[LICENSE](LICENSE)文件。

## 参考资料

*   [模型上下文协议](https://modelcontextprotocol.org/)

