experimental.tasks.getTaskResult() throws TypeError: Cannot read properties of undefined (reading '_zod') when the optional result schema is omitted

Đang mở Phù hợp với người mới
#2,742 2 bình luận 0 reaction 0 người được giao Xem trên GitHub

Chưa có ai nhận issue này.

Đánh giá

Độ khó
2/5
Thời gian dự kiến
1-3 giờ
Mức phù hợp với người mới
84/100
Loại issue
Lỗi
Độ rõ ràng
Đặc tả rõ ràng
Mức độ hoạt động
Sôi nổi
Công nghệ
typescript
Lĩnh vực
api

Hướng nghiên cứu

Bắt đầu trong src/experimental/tasks/client.ts và so sánh getTaskResult với cách xử lý schema mặc định trong getTask và listTasks. Theo dõi schema được chuyển tiếp qua Protocol.getTaskResult trong src/shared/protocol.ts và tái hiện vấn đề bằng ví dụ InMemoryTransport được cung cấp. Hoàn tất khi gọi getTaskResult(taskId) mà không có schema không còn gây ra lỗi, trong khi cách khắc phục bằng schema tường minh vẫn được hỗ trợ.

Do mô hình lập chỉ mục viết ra từ nội dung của issue.

Mô tả

Summary

client.experimental.tasks.getTaskResult(taskId) throws TypeError: Cannot read properties of undefined (reading '_zod') when called without an explicit result schema, which its type signature says is optional.

ExperimentalClientTasks.getTaskResult declares resultSchema?: T (src/experimental/tasks/client.ts) and forwards it to Protocol.getTaskResult, whose signature requires it (resultSchema: T) and passes it straight into request(). With undefined, validation reaches isZ4Schema(undefined) in src/server/zod-compat.ts, which reads ._zod off it.

Because the throw happens in _onresponse — inside the transport's message handler rather than in the awaited call path — it is also awkward to catch from application code.

Compare getTask and listTasks, which both pass a default schema; getTaskResult appears to have been missed.

Reproduction
import { Client } from "@modelcontextprotocol/sdk/client/index.js";
import { Server } from "@modelcontextprotocol/sdk/server/index.js";
import { InMemoryTransport } from "@modelcontextprotocol/sdk/inMemory.js";
import { InMemoryTaskStore } from "@modelcontextprotocol/sdk/experimental/tasks/stores/in-memory.js";
import { CallToolRequestSchema, ListToolsRequestSchema } from "@modelcontextprotocol/sdk/types.js";

const store = new InMemoryTaskStore();
const server = new Server(
  { name: "repro", version: "1.0.0" },
  {
    capabilities: { tools: {}, tasks: { list: {}, cancel: {}, requests: { tools: { call: {} } } } },
    taskStore: store,
  },
);
server.setRequestHandler(ListToolsRequestSchema, () => ({
  tools: [{ name: "t", inputSchema: { type: "object" } }],
}));
server.setRequestHandler(CallToolRequestSchema, async (request, extra) => {
  const task = await extra.taskStore.createTask({ ttl: 60000 });
  await extra.taskStore.storeTaskResult(task.taskId, "completed", {
    content: [{ type: "text", text: "done" }],
  });
  return { task };
});

const [clientTransport, serverTransport] = InMemoryTransport.createLinkedPair();
const client = new Client({ name: "repro-client", version: "1.0.0" });
await Promise.all([server.connect(serverTransport), client.connect(clientTransport)]);

let taskId = "";
for await (const message of client.experimental.tasks.callToolStream({
  name: "t",
  arguments: {},
  task: { ttl: 60000 },
})) {
  if (message.type === "taskCreated") { taskId = message.task.taskId; break; }
  if (message.type === "result") { taskId = message.result.task?.taskId ?? ""; break; }
}

await client.experimental.tasks.getTaskResult(taskId); // TypeError
Expected

Either getTaskResult defaults to CallToolResultSchema (or ResultSchema) the way getTask and listTasks default theirs, or the parameter is made required so the type signature matches the behaviour.

Actual
TypeError: Cannot read properties of undefined (reading '_zod')
    at isZ4Schema (src/server/zod-compat.ts:60)
    at safeParse (src/server/zod-compat.ts:82)
    at src/shared/protocol.ts:1199
    at Client._onresponse (src/shared/protocol.ts:928)
Workaround

Pass the schema explicitly:

import { CallToolResultSchema } from "@modelcontextprotocol/sdk/types.js";
await client.experimental.tasks.getTaskResult(taskId, CallToolResultSchema);
Environment
  • @modelcontextprotocol/sdk 1.30.0
  • Node 22.23.1, macOS
  • Both zod@3.25.76 and zod@4.1.5 present in the workspace; reproduced via the v3-resolved copy.

Happy to open a PR adding the default if that is the direction you would prefer.

Ngôn ngữ chính
TypeScript
Star
13.4k
Fork
2.2k
Merge trung bình
3 ngày 12 giờ
Pull request đã merge (30 ngày)
3

Hướng dẫn đóng góp

Mở hướng dẫn đóng góp

Bắt đầu từ đâu

  1. Đọc hết issue, rồi đọc hướng dẫn đóng góp của dự án.
  2. Bình luận trên issue rằng bạn sẽ nhận — tránh hai người làm cùng một việc.
  3. Fork repository và làm thay đổi trên một nhánh.
  4. Mở pull request có tham chiếu số hiệu của issue.

Issue khác của modelcontextprotocol/typescript-sdk

Tất cả issue của modelcontextprotocol/typescript-sdk

Issue tương tự

Thêm issue về TypeScript

Nhận issue mới trong hộp thư của bạn

Bản tóm tắt ngắn những issue GitHub phù hợp với người mới.