official-mcp-auth-port.ts 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. /* Agent 侧的官方 MCP 身份头端口。
  2. Agent 进程不是身份权威:经 interaction/requestOfficialMcpAuthHeaders 反向请求 host,
  3. 由 host 解析当前 Coding Plan 凭证后回传本次请求的身份头。凭证不落 runtime config、
  4. 不持久化、不进日志。 */
  5. import {
  6. zcodeOfficialMcpAuthHeadersResponseSchema,
  7. zcodeProtocolMethods,
  8. type ZCodeWorkspaceRef,
  9. } from "@zcode/shared";
  10. import type { OfficialMcpAuthHeadersPort } from "@zcode/contracts";
  11. import type { ZCodeProtocolAgentServerContext } from "./server-types.js";
  12. let requestSequence = 0;
  13. /** 端口只需要发反向请求的能力,不需要整个 server context。 */
  14. export type OfficialMcpAuthRequestContext = Pick<
  15. ZCodeProtocolAgentServerContext,
  16. "requestClient"
  17. >;
  18. /**
  19. * 构造经协议反向请求取身份头的端口。
  20. *
  21. * `resolveContext` 是惰性的:MCP 连接池的构造早于 ZCodeProtocolAgentServer,
  22. * server 就绪前返回 undefined,此时按不可用处理(不降级为匿名请求)。
  23. *
  24. * 失败一律走返回值(ok:false + 可枚举 reason),不抛异常:
  25. * 传输层异常统一归为 official_auth_unavailable,由 MCP adapter 置该 server 为 failed。
  26. *
  27. * workspace 的用途与已知缺口:
  28. * - host 侧**不**用它路由响应——响应经 `client.respond(request.id, ...)` 回到发起请求的
  29. * 那条 stdio 连接,路由由连接本身决定。该字段仅作请求上下文/审计用;
  30. * - 但仍必须遵守仓库约定 `workspaceKey = workspaceIdentity?.trim() || workspacePath`,
  31. * 否则同路径不同 identity 的远端 workspace 在审计上下文里无法区分;
  32. * - **剩余缺口**:CLI agent 进程当前没有 workspaceIdentity 来源
  33. * (`RunZCodeProtocolAgentOptions` 只有 cwd/env/…,也无对应环境变量),因此该字段实际
  34. * 退化为 workspacePath。这里保证的是"拿到 identity 就正确透传",而不是"identity 一定存在"。
  35. * 若将来审计需要真实远端身份,需在 spawn 或协议层把 identity 传给 agent,不在本阶段范围。
  36. */
  37. export function createOfficialMcpAuthHeadersPort(input: {
  38. resolveContext: () => OfficialMcpAuthRequestContext | undefined;
  39. resolveWorkspace: (input: {
  40. workspaceIdentity?: string;
  41. workspacePath?: string;
  42. }) => ZCodeWorkspaceRef | undefined;
  43. }): OfficialMcpAuthHeadersPort {
  44. return {
  45. async resolveHeaders(request) {
  46. const context = input.resolveContext();
  47. const workspace = input.resolveWorkspace({
  48. ...(request.workspaceIdentity ? { workspaceIdentity: request.workspaceIdentity } : {}),
  49. ...(request.workspacePath ? { workspacePath: request.workspacePath } : {}),
  50. });
  51. if (!context || !workspace) {
  52. return { ok: false, reason: "official_auth_unavailable" };
  53. }
  54. requestSequence += 1;
  55. try {
  56. return await context.requestClient(
  57. zcodeProtocolMethods.interactionRequestOfficialMcpAuthHeaders,
  58. {
  59. mcpKey: request.mcpKey,
  60. pluginId: request.pluginId,
  61. requestId: `official-mcp-auth:${requestSequence}`,
  62. targetOrigin: request.targetOrigin,
  63. workspace,
  64. },
  65. zcodeOfficialMcpAuthHeadersResponseSchema,
  66. request.signal ? { signal: request.signal } : {},
  67. );
  68. } catch {
  69. // host 不可达/协议错误:按不可用处理。错误详情不带出(可能含请求上下文),
  70. // host 侧已记录不含秘密的分类日志。
  71. return { ok: false, reason: "official_auth_unavailable" };
  72. }
  73. },
  74. };
  75. }