modelcontextprotocol / modelcontextprotocol/typescript-sdk

[v2] Typescript: optional content when structuredContent is present

Open
#2,755 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

v2
Dominant language
TypeScript
Stars
13.4k
Forks
2.2k
Avg merge
3d 15h
Merged PRs (30d)
4

Description

What happened?

If I create a tool with a structuredContent but no content, I get a typescript error saying that the content field is mandatory. But this is stupid for two reasons:

  1. For now at least, it seems like the SDK will anyway add a content automatically if not present… So let me save time (and redundancy) by avoiding to explicitly add it myself.

  2. Worst, according to the spec:

    For backwards compatibility, a tool that returns structured content SHOULD also return the serialized JSON in a TextContent block.

    The verb SHOULD is used, and not MUST, which means that we don't strictly need to follow this rule if we don't want to be backward compatible.

What did you expect?

I'd expect typescript to allow tools to return no content but only a structuredContent. Moreover, we should add an option to say if the SDK should add itself a serialization of the structuredContent into content as this is done for now: I believe that it is a good thing to enable it by default as it is done now, but for efficiency reasons it would be great to disable content and only keep structuredContent (at least in v2) since this is allowed by the specification.

Code to reproduce
server.registerTool(
    'ping',
    {
      description: 'Returns a string pong with a timestamp',
      outputSchema: z.string()
    },
    async () => {
      const str = `Pong at ${new Date()}`
      return {
        // content: [{ type: 'text', text: str }], // Error without this line
        structuredContent: str
      };
    }
  );
SDK version

2.0.0

Area

Server

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start at the server.registerTool entry point and the TypeScript types for tool results, then trace how structuredContent is serialized into content. Use the provided ping reproduction to verify that structuredContent-only results type-check and that the serialization behavior can be controlled as requested.

Written by the indexing model from the issue text.

Assessment

Tech stack
typescript
Domain
api, backend
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Active
Clarity
Mostly clear
Newbie friendliness
62/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.