共用方式為


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

命令列選項

名稱 Description 預設
--no-stdio-mocks 停用載入 STDIO 模擬回應 false
--stdio-mocks-file 包含 STDIO 模擬回應的檔案路徑 -

模擬檔案範例

以下是 STDIO 模擬物件的範例。

回覆標準

對包含特定文字的標準版回覆,請以標準版回應。

檔案: 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回應

回覆 stdin 並以錯誤訊息回應 stderr。

檔案: 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.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 申請身分證
@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 次發生時回應

只有在攔截匹配的標準第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 以匹配的物件 yes
response 定義要傳回之回應的回應物件 yes

請求物件

每個要求都有下列屬性:

房產 Description 為必填項目 預設值 範例值
bodyFragment 一個應該存在於 stdin 的字串 yes tools/list
nth 只有在攔截請求第 n 次時才回應 no 2

Response 物件

每個回應都有下列屬性:

房產 Description 為必填項目 預設值 範例值
stdout 內容要送給標準學生 no {"result": "ok"}\n
stderr 內容要送給 stderr no Error: Something went wrong\n

回應意見

如果你想從檔案載入回應內容,可以將 or stderr 屬性設stdout為字串值,開頭@是檔案路徑相對於 mocks 檔案。 例如,會 @response.json 回傳與 mocks 檔案同一個目錄中儲存 response.json 的內容。

若要動態包含 stdin 的值,請使用 @stdin.body.* 佔位符。 例如,會 @stdin.body.id 回傳 stdin JSON 中屬性 id 的值。

插件備註

此外掛設計用於stdio攔截並模擬 STDIO 與本地執行檔的通訊。 它對於測試和除錯模型上下文協定(MCP)伺服器及其他基於 STDIO 的應用程式非常有用。

blockUnmockedRequests 設定為 true時,任何與 mock 不符的 stdin 都會被消耗,而不會轉發給子程序。 當你想完全模擬執行檔的行為而不執行其邏輯時,阻擋未模擬請求非常有用。

後續步驟