modelcontextprotocol / modelcontextprotocol/typescript-sdk
[v2] Typescript: optional content when structuredContent is present
Nobody has claimed this yet.
- 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:
-
For now at least, it seems like the SDK will anyway add a
contentautomatically if not present… So let me save time (and redundancy) by avoiding to explicitly add it myself. -
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
SHOULDis used, and notMUST, 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
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- 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