Skip to content

fix(memory): remove Zod-based outputSchema to fix serialization failures#3966

Open
ianliuy wants to merge 1 commit intomodelcontextprotocol:mainfrom
ianliuy:fix/issue-3622
Open

fix(memory): remove Zod-based outputSchema to fix serialization failures#3966
ianliuy wants to merge 1 commit intomodelcontextprotocol:mainfrom
ianliuy:fix/issue-3622

Conversation

@ianliuy
Copy link
Copy Markdown

@ianliuy ianliuy commented Apr 16, 2026

What's broken?

The memory server's tool definitions pass Zod schema objects directly to outputSchema, which violates the MCP spec requiring plain JSON Schema objects. This causes _zod serialization errors when the tools/list endpoint serializes schemas to JSON-RPC responses.

Who is affected?

Projects using the npm @modelcontextprotocol/sdk directly (rather than Claude Desktop's bundled SDK) encounter serialization failures when calling tools/list or invoking tools with outputSchema defined.

Root cause

outputSchema fields use Zod raw shapes like:

outputSchema: {
  entities: z.array(EntitySchema),
  relations: z.array(RelationSchema)
}

The MCP spec requires these to be plain JSON Schema objects.

Why not just replace with plain JSON Schema?

I investigated replacing Zod schemas with hand-written JSON Schema objects (the approach suggested in the issue). However, the current SDK (v1.26.0–1.29.0) cannot accept plain JSON Schema for outputSchema:

  1. tools/list silently drops itnormalizeObjectSchema() returns undefined for non-Zod objects, so outputSchema never appears in the response
  2. Tool calls crashvalidateToolOutput() calls safeParseAsync(undefined, ...) which throws Cannot read properties of undefined (reading '_zod')

This is a TypeScript SDK limitation, not a spec issue. Until the SDK adds native JSON Schema passthrough for outputSchema, the safest fix is to remove it entirely.

Fix

Remove outputSchema and structuredContent from all 9 tool registrations. Tools continue to return data via the content array (JSON text), preserving full functionality. The only change is that tools/list no longer advertises output schemas for these tools.

Testing

  • npm run build passes (TypeScript compilation clean)
  • Verified at runtime that tools still return correct JSON data via content
  • Verified that removing outputSchema skips output validation (no crashes)

Fixes #3622

Remove outputSchema and structuredContent from all 9 tool registrations
in the memory server. The Zod schema objects passed to outputSchema
violate the MCP spec (which requires plain JSON Schema) and cause
_zod serialization errors with the npm SDK.

The current SDK (1.26.0+) only accepts Zod types for outputSchema
and cannot pass through plain JSON Schema objects (they get silently
dropped from tools/list and cause crashes in output validation).
Removing outputSchema entirely is the safest fix until the SDK adds
native JSON Schema support for outputSchema.

All tool responses still return data via the content array (JSON text),
so no functionality is lost.

Fixes modelcontextprotocol#3622

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Signed-off-by: Yiyang Liu <37043548+ianliuy@users.noreply.github.com>
@cliffhall cliffhall added bug Something isn't working server-memory Reference implementation for the Memory MCP server - src/memory labels Apr 20, 2026
@cliffhall
Copy link
Copy Markdown
Member

@claude review

@claude
Copy link
Copy Markdown
Contributor

claude Bot commented Apr 21, 2026

Claude finished @cliffhall's task in 1m 35s —— View job


PR Review

  • Read changed files and diff
  • Verify the fix approach is correct
  • Check for correctness, completeness, and edge cases
  • Post review findings

Summary

This PR removes outputSchema and structuredContent from all 9 tool registrations in the memory server to fix serialization crashes. The diagnosis and fix are correct. Below are my findings.


What the PR does

The original code passed raw Zod schema objects (e.g. z.array(EntitySchema)) as the outputSchema field in registerTool(). The MCP spec requires plain JSON Schema objects there. The SDK's tools/list serializer tries to walk these Zod objects as if they were JSON Schema and fails with _zod-related errors. The PR removes outputSchema from all tool definitions and removes the corresponding structuredContent from all tool return values.


Correctness

The fix is correct and complete. All 9 affected tools are consistently updated:

Tool outputSchema removed structuredContent removed
create_entities
create_relations
add_observations
delete_entities
delete_observations
delete_relations
read_graph
search_nodes
open_nodes

Removing structuredContent from the return values is also correct — per the MCP spec, structuredContent is only meaningful when outputSchema is defined.

All tools continue returning data via content[].text (JSON-serialized), so no functionality is lost from the caller's perspective.


Concerns

1. This is a feature regression, not a definitive fix.

The memory server was one of the few places in this codebase intentionally demonstrating MCP's structured tool output feature (outputSchema + structuredContent). That capability is now silently gone. The PR description explains the SDK limitation accurately — normalizeObjectSchema() drops plain JSON Schema objects and validateToolOutput() crashes on them — but there is no tracking issue or comment pointing toward restoring this when the SDK is updated. I'd recommend opening a follow-up issue to restore structured output once the SDK supports plain JSON Schema for outputSchema.

2. The PR description overstates the SDK constraint slightly.

The PR says the SDK "cannot accept plain JSON Schema for outputSchema." This is a current SDK implementation limitation, not a permanent one. The framing "until the SDK adds native JSON Schema passthrough" is accurate, but worth making explicit in the commit or a linked issue so future maintainers know structured output should come back here.

3. No new tests.

There are no tests for tool registration metadata (i.e., no test that asserts outputSchema is absent or that tools/list serializes cleanly). The existing tests in __tests__/knowledge-graph.test.ts only cover KnowledgeGraphManager directly and still pass. While adding a regression test for the serialization failure would strengthen this fix, it's not a blocker.


Minor notes

  • The diff is clean and minimal — no unrelated changes.
  • The package.json specifies @modelcontextprotocol/sdk: ^1.26.0, matching the affected range in the PR description.
  • TypeScript compilation is clean (no type errors introduced, as structuredContent removal is type-safe).

Verdict

The fix correctly resolves the immediate serialization crash. The approach is the right call given the SDK limitation. The main ask before merging: create or reference a follow-up issue to restore outputSchema with proper plain JSON Schema once the SDK supports it, so this regression doesn't become permanent. Everything else looks good.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Something isn't working server-memory Reference implementation for the Memory MCP server - src/memory

Projects

None yet

Development

Successfully merging this pull request may close these issues.

outputSchema uses Zod objects instead of JSON Schema, violating spec and causing serialization failures

2 participants