MockStdioResponsePlugin

模拟基于 STDIO 的应用程序(例如模型上下文协议(MCP)服务器的响应。

插件实例定义

{
  "name": "MockStdioResponsePlugin",
  "enabled": true,
  "pluginPath": "~appFolder/plugins/DevProxy.Plugins.dll",
  "configSection": "mockStdioResponsePlugin"
}

配置示例

{
  "$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"
    }
  ],
  "mockStdioResponsePlugin": {
    "$schema": "https://raw.githubusercontent.com/dotnet/dev-proxy/main/schemas/v2.1.0/mockstdioresponseplugin.schema.json",
    "mocksFile": "stdio-mocks.json"
  }
}

配置属性

资产 Description 违约
mocksFile 包含 STDIO 模拟响应的文件的路径 stdio-mocks.json
blockUnmockedRequests true,防止不匹配的 stdin 到达子进程 false

命令行选项

Name Description 违约
--no-stdio-mocks 禁用加载 STDIO 模拟响应 false
--stdio-mocks-file 包含 STDIO 模拟响应的文件的路径 -

模拟文件示例

下面是 STDIO 模拟对象的示例。

使用 stdout 做出响应

响应包含具有 stdout 响应的特定文本的 stdin。

文件: stdio-mocks.json

{
  "$schema": "https://raw.githubusercontent.com/dotnet/dev-proxy/main/schemas/v2.1.0/mockstdioresponseplugin.schema.json",
  "mocks": [
    {
      "request": {
        "bodyFragment": "tools/list"
      },
      "response": {
        "stdout": "{\"jsonrpc\":\"2.0\",\"id\":1,\"result\":{\"tools\":[]}}\n"
      }
    }
  ]
}

使用 stderr 做出响应

使用 stderr 上的错误消息响应 stdin。

文件: stdio-mocks.json

{
  "$schema": "https://raw.githubusercontent.com/dotnet/dev-proxy/main/schemas/v2.1.0/mockstdioresponseplugin.schema.json",
  "mocks": [
    {
      "request": {
        "bodyFragment": "invalid_method"
      },
      "response": {
        "stderr": "Error: Unknown method\n"
      }
    }
  ]
}

使用 stdin 中的占位符

使用 @stdin.body.* 占位符在响应中动态包含 stdin 中的值。

文件: stdio-mocks.json

{
  "$schema": "https://raw.githubusercontent.com/dotnet/dev-proxy/main/schemas/v2.1.0/mockstdioresponseplugin.schema.json",
  "mocks": [
    {
      "request": {
        "bodyFragment": "tools/list"
      },
      "response": {
        "stdout": "{\"jsonrpc\":\"2.0\",\"id\":@stdin.body.id,\"result\":{\"tools\":[]}}\n"
      }
    }
  ]
}

以下占位符可用:

Placeholder Description
@stdin.body.id JSON-RPC 请求 ID
@stdin.body.method JSON-RPC 方法名称
@stdin.body.params.name 嵌套属性访问

从文件加载响应

使用 @filename 语法从外部文件加载模拟响应内容。

文件: stdio-mocks.json

{
  "$schema": "https://raw.githubusercontent.com/dotnet/dev-proxy/main/schemas/v2.1.0/mockstdioresponseplugin.schema.json",
  "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"}}}

响应第 n 个匹配项

仅在截获匹配 stdin 第 n 次后做出响应。

文件: stdio-mocks.json

{
  "$schema": "https://raw.githubusercontent.com/dotnet/dev-proxy/main/schemas/v2.1.0/mockstdioresponseplugin.schema.json",
  "mocks": [
    {
      "request": {
        "bodyFragment": "tools/call",
        "nth": 2
      },
      "response": {
        "stdout": "{\"jsonrpc\":\"2.0\",\"id\":@stdin.body.id,\"result\":{\"content\":[{\"type\":\"text\",\"text\":\"Operation completed\"}]}}\n"
      }
    }
  ]
}

模拟文件属性

资产 Description 必选
request 定义要匹配的 stdin 的请求对象 是的
response 定义要返回的响应的响应对象 是的

请求对象

每个请求具有以下属性:

资产 Description 必选 默认值 示例值
bodyFragment 应存在于 stdin 中的字符串 是的 tools/list
nth 仅在截获第 n 次请求时做出响应 2

响应对象

每个响应具有以下属性:

资产 Description 必选 默认值 示例值
stdout 要发送到 stdout 的内容 {"result": "ok"}\n
stderr 要发送到 stderr 的内容 Error: Something went wrong\n

响应备注

如果要从文件加载响应内容,请将 stdoutstderr 属性设置为字符串值,该值以 @ 相对于 mocks 文件的文件路径开头。 例如, @response.json 返回存储在文件与 response.json mocks 文件相同的目录中的内容。

若要在响应中动态包含 stdin 中的值,请使用 @stdin.body.* 占位符。 例如, @stdin.body.id 从 stdin JSON 返回属性的值 id

插件备注

此插件旨在与命令一起使用stdio,以截获和模拟与本地可执行文件的 STDIO 通信。 它可用于测试和调试模型上下文协议(MCP)服务器和其他基于 STDIO 的应用程序。

如果 设置为/>,则使用与模拟不匹配的任何 stdin,并且不会转发到子进程。 如果想要完全模拟可执行文件的行为而不运行其实际逻辑,则阻止未模拟的请求非常有用。

后续步骤