跳到主要内容

Lens Assistant

后端 API​

Lens Assistant 由 server/mcp/ 下的 Node MCP 服务器支撑,而不是由一个专门的 FastAPI 路由器支撑。server/server.js 通过 app.use(mcpRouter) 直接挂载 mcpRouter,而 server/mcp/router.ts 在 /api/mcp 上暴露无状态的 Streamable HTTP 端点。

发送一次无状态的 MCP 请求​

通过创建一个全新的 McpServer、建立连接并处理请求体,来处理一次无状态的 Streamable HTTP MCP 请求。

POST /api/mcp

请求:

  • Content type:application/json
  • Body:一个 MCP JSON-RPC 请求对象。在无状态模式下,Lens 会为每个请求创建一个全新的服务器,因此不需要会话 ID。
  • 常见的请求形态包括 initialize、tools/list 和 tools/call。

tools/call 请求示例:

{
"jsonrpc": "2.0",
"id": "req-2",
"method": "tools/call",
"params": {
"name": "list_simulation_backends",
"arguments": {}
}
}

响应:

成功的响应使用 MCP JSON-RPC 结果信封格式。对于工具调用,Lens 的 MCP 服务器会返回 content 块,其 text 字段包含经过 JSON 字符串化的 Lens REST 结果。

{
"jsonrpc": "2.0",
"id": "req-2",
"result": {
"content": [
{
"type": "text",
"text": "{\n \"items\": []\n}"
}
]
}
}

如果在 SDK 发送响应之前发生了未处理的服务器错误,Lens 会返回:

{
"jsonrpc": "2.0",
"error": {
"code": -32603,
"message": "Internal server error"
},
"id": null
}

无状态模式下不支持 GET​

返回 405 Method not allowed.,因为无状态模式不支持 Streamable HTTP 的 GET 流。

GET /api/mcp

请求:

该端点没有请求体。在 Lens 的无状态 MCP 模式下,Streamable HTTP 的 GET 流被特意禁用。

响应:

返回一个 HTTP 状态码为 405 的 JSON-RPC 错误负载。

{
"jsonrpc": "2.0",
"error": {
"code": -32000,
"message": "Method not allowed."
},
"id": null
}

无状态模式下不支持 DELETE​

返回 405 Method not allowed.,因为无状态模式不支持终止会话。

DELETE /api/mcp

请求:

该端点没有请求体。Lens 不会在请求之间保留 MCP 会话,因此也就没有服务器端会话可以终止。

响应:

返回一个 HTTP 状态码为 405 的 JSON-RPC 错误负载。

{
"jsonrpc": "2.0",
"error": {
"code": -32000,
"message": "Method not allowed."
},
"id": null
}

工具目录由两部分组成:server/mcp/tools.ts 包含 138 个自动生成的工具,server/mcp/specialTools.ts 包含 25 个手写工具。自动生成的那一半由 node scripts/generate-mcp-tools.mjs 根据 llm_d_lens FastAPI 的 OpenAPI schema 生成,而手写的那一半涵盖了组合型或 Node 支撑的路由。这些处理程序都是对 Lens 自身 REST API 的瘦封装:server/mcp/internal.ts 通过 internalRequest(...) 将请求转发到环回地址的 /api/... 路径上。

MCP 工具调用的内容​

具有代表性的自动生成 MCP 工具会直接转发到已有的 Python 支撑的 REST 模块:

MCP 工具转发的 HTTP 端点底层模块
create_standard_vllm_deployment_runPOST /api/v1/deployments/standard-vllm-runs部署,经由 llm_d_lens/deploy/router.py
list_deployment_executionsGET /api/v1/deployments/executions部署,经由 llm_d_lens/deploy/router.py
create_evaluate_runPOST /api/v1/evaluate/runs评估,经由 llm_d_lens/evaluation/router.py
get_cluster_stack_statusGET /api/v1/monitoring/cluster-stack/status可观测性,经由 llm_d_lens/monitoring/cluster_stack/router.py
list_simulation_tasksGET /api/simulation/tasks模拟,经由 llm_d_lens/simulation/router.py

如果你需要的是 Python 而不是 MCP 传输,请直接调用其背后的 Python 领域逻辑,而不是通过 /api/mcp。例如,MCP 工具 create_standard_vllm_deployment_run 最终调用的是与 POST /api/v1/deployments/standard-vllm-runs 相同的部署辅助函数:

from llm_d_lens.deploy.standard_kubernetes_service import (
StandardKubernetesServiceRequest,
build_standard_kubernetes_service_configuration,
)

request = StandardKubernetesServiceRequest(
model="meta-llama/Llama-3.1-8B-Instruct",
deployment_name="assistant-backed-deploy",
cluster_session_id="123e4567-e89b-12d3-a456-426614174000",
replicas=1,
tensor_parallel_size=1,
)
configuration = build_standard_kubernetes_service_configuration(request)

print(configuration.checksum)

完整的请求/响应 schema 请参见 FastAPI 的 OpenAPI schema(使用 npm run docs:api 重新生成)。