模拟 MCP 服务器的 STDIO 响应

新式 AI 开发越来越依赖于通过 STDIO(尤其是模型上下文协议(MCP)服务器进行通信的本地工具。 这些服务器通过 stdin 接收 JSON-RPC 请求,并通过 stdout 发送 JSON-RPC 响应。 使用开发代理,可以截获和模拟 STDIO 通信以测试 AI 客户端应用程序,而无需运行实际的服务器逻辑。

先决条件

模拟 MCP 服务器响应

若要模拟 MCP 服务器响应,请使用 stdio 命令和 MockStdioResponsePlugin. 插件截获 stdin 并通过 stdout 或 stderr 返回模拟响应。

1.创建开发代理配置文件

创建包含以下内容的 devproxyrc.json 文件:

{
  "$schema": "https://raw.githubusercontent.com/dotnet/dev-proxy/main/schemas/v2.1.0/rc.schema.json",
  "plugins": [
    {
      "name": "MockSTDIOResponsePlugin",
      "enabled": true,
      "pluginPath": "~appFolder/plugins/DevProxy.Plugins.dll",
      "configSection": "mockSTDIOResponsePlugin"
    },
    {
      "name": "DevToolsPlugin",
      "enabled": true,
      "pluginPath": "~appFolder/plugins/DevProxy.Plugins.dll",
      "configSection": "devTools"
    }
  ],
  "devTools": {
    "preferredBrowser": "Edge"
  },
  "mockStdioResponsePlugin": {
    "mocksFile": "stdio-mocks.json"
  }
}

2.创建模拟文件

为 MCP 服务器创建一个包含模拟响应的文件:

{
  "$schema": "https://raw.githubusercontent.com/dotnet/dev-proxy/main/schemas/v2.1.0/mockstdioresponseplugin.schema.json",
  "mocks": [
    {
      "request": {
        "bodyFragment": "initialize"
      },
      "response": {
        "stdout": "{\"jsonrpc\":\"2.0\",\"id\":@stdin.body.id,\"result\":{\"protocolVersion\":\"2024-11-05\",\"capabilities\":{\"tools\":{}},\"serverInfo\":{\"name\":\"Mock MCP Server\",\"version\":\"1.0.0\"}}}\n"
      }
    },
    {
      "request": {
        "bodyFragment": "tools/list"
      },
      "response": {
        "stdout": "{\"jsonrpc\":\"2.0\",\"id\":@stdin.body.id,\"result\":{\"tools\":[{\"name\":\"get_weather\",\"description\":\"Get current weather for a location\",\"inputSchema\":{\"type\":\"object\",\"properties\":{\"location\":{\"type\":\"string\",\"description\":\"City name\"}},\"required\":[\"location\"]}}]}}\n"
      }
    },
    {
      "request": {
        "bodyFragment": "tools/call"
      },
      "response": {
        "stdout": "{\"jsonrpc\":\"2.0\",\"id\":@stdin.body.id,\"result\":{\"content\":[{\"type\":\"text\",\"text\":\"Mock response from the tool\"}]}}\n"
      }
    }
  ]
}

3.启动开发代理

使用 STDIO 命令运行开发代理,指定要运行的命令:

devproxy stdio npx -y @modelcontextprotocol/server-filesystem

开发代理将 MCP 服务器作为子进程启动,并截获所有 stdin/stdout 通信。 当 stdin 包含与模拟 bodyFragment文本匹配的文本时,开发代理将返回模拟响应,而不是将请求转发到实际服务器。

对动态响应使用占位符

若要创建包含请求中的值的动态响应,请使用 @stdin.body.* 占位符:

{
  "mocks": [
    {
      "request": {
        "bodyFragment": "echo"
      },
      "response": {
        "stdout": "{\"jsonrpc\":\"2.0\",\"id\":@stdin.body.id,\"result\":{\"message\":\"You said: @stdin.body.params.text\"}}\n"
      }
    }
  ]
}

可用占位符:

Placeholder Description
@stdin.body.id JSON-RPC 请求 ID
@stdin.body.method JSON-RPC 方法名称
@stdin.body.params.* 访问请求参数

阻止未模拟的请求

若要防止未模拟的请求到达子进程,请将blockUnmockedRequests设置为true

{
  "mockStdioResponsePlugin": {
    "mocksFile": "stdio-mocks.json",
    "blockUnmockedRequests": true
  }
}

如果想要完全模拟 MCP 服务器而不运行其实际逻辑,则阻止未模拟的请求非常有用。

从文件加载模拟响应

对于复杂的响应,请从外部文件加载内容:

{
  "mocks": [
    {
      "request": {
        "bodyFragment": "initialize"
      },
      "response": {
        "stdout": "@initialize-response.json"
      }
    }
  ]
}

创建 initialize-response.json 文件:

{"jsonrpc":"2.0","id":1,"result":{"protocolVersion":"2024-11-05","capabilities":{"tools":{}},"serverInfo":{"name":"Mock MCP Server","version":"1.0.0"}}}

检查 DevTools 中的 STDIO 流量

启用 DevToolsPlugin时,开发代理将打开 Chrome DevTools,可在其中检查所有 STDIO 通信:

  • 网络选项卡:查看所有 stdin/stdout/stderr 消息
  • URL:消息显示为 stdio://command-name
  • 方法:请求显示为 stdin
  • 状态代码stdout 显示为 200, stderr 显示为 500
  • 计时:查看每个请求/响应花费的时间

使用 DevToolsPlugin 对于调试 AI 客户端与 MCP 服务器之间的通信问题是无价的。

模拟延迟

若要测试应用程序如何处理慢的 MCP 服务器响应,请添加:LatencyPlugin

{
  "$schema": "https://raw.githubusercontent.com/dotnet/dev-proxy/main/schemas/v2.1.0/rc.schema.json",
  "plugins": [
    {
      "name": "LatencyPlugin",
      "enabled": true,
      "pluginPath": "~appFolder/plugins/DevProxy.Plugins.dll",
      "configSection": "latencyPlugin"
    },
    {
      "name": "MockStdioResponsePlugin",
      "enabled": true,
      "pluginPath": "~appFolder/plugins/DevProxy.Plugins.dll",
      "configSection": "mockStdioResponsePlugin"
    }
  ],
  "latencyPlugin": {
    "minMs": 100,
    "maxMs": 500
  },
  "mockStdioResponsePlugin": {
    "mocksFile": "stdio-mocks.json"
  }
}

后续步骤

详细了解 STDIO 代理功能: