Skip to content

Commit 7bdfcc6

Browse files
Add a tool with Structured Content and an Output Schema
Tools gained Structured Content in Specification 2025-16-18 https://modelcontextprotocol.io/specification/2025-06-18/server/tools#structured-content The MCP Inspector is able to handle these outputs.
1 parent 7c2a06d commit 7bdfcc6

2 files changed

Lines changed: 64 additions & 0 deletions

File tree

src/everything/README.md

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,15 @@ This MCP server attempts to exercise all the features of the MCP protocol. It is
7272
- Embedded resource with `type: "resource"`
7373
- Text instruction for using the resource URI
7474

75+
9. `structuredContent`
76+
- Demonstrates a tool returning structured content using the example in the specification
77+
- Provides an output schema to allow testing of client SHOULD advisory to validate the result using the schema
78+
- Inputs:
79+
- `location` (string): A location or ZIP code, mock data is returned regardless of value
80+
- Returns: a response with
81+
- `structuredContent` field conformant to the output schema
82+
- A backward compatible Text Content field, a SHOULD advisory in the specification
83+
7584
### Resources
7685

7786
The server provides 100 test resources in two formats:

src/everything/everything.ts

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,9 @@ const instructions = readFileSync(join(__dirname, "instructions.md"), "utf-8");
3131
const ToolInputSchema = ToolSchema.shape.inputSchema;
3232
type ToolInput = z.infer<typeof ToolInputSchema>;
3333

34+
const ToolOutputSchema = ToolSchema.shape.outputSchema;
35+
type ToolOutput = z.infer<typeof ToolOutputSchema>;
36+
3437
/* Input schemas for tools implemented in this server */
3538
const EchoSchema = z.object({
3639
message: z.string().describe("Message to echo"),
@@ -82,6 +85,28 @@ const GetResourceReferenceSchema = z.object({
8285
.describe("ID of the resource to reference (1-100)"),
8386
});
8487

88+
const StructuredContentSchema = {
89+
input: z.object({
90+
location: z
91+
.string()
92+
.trim()
93+
.min(1)
94+
.describe("City name or zip code"),
95+
}),
96+
97+
output: z.object({
98+
temperature: z
99+
.number()
100+
.describe("Temperature in celsius"),
101+
conditions: z
102+
.string()
103+
.describe("Weather conditions description"),
104+
humidity: z
105+
.number()
106+
.describe("Humidity percentage"),
107+
})
108+
};
109+
85110
enum ToolName {
86111
ECHO = "echo",
87112
ADD = "add",
@@ -91,6 +116,7 @@ enum ToolName {
91116
GET_TINY_IMAGE = "getTinyImage",
92117
ANNOTATED_MESSAGE = "annotatedMessage",
93118
GET_RESOURCE_REFERENCE = "getResourceReference",
119+
STRUCTURED_CONTENT = "structuredContent"
94120
}
95121

96122
enum PromptName {
@@ -463,6 +489,13 @@ export const createServer = () => {
463489
"Returns a resource reference that can be used by MCP clients",
464490
inputSchema: zodToJsonSchema(GetResourceReferenceSchema) as ToolInput,
465491
},
492+
{
493+
name: ToolName.STRUCTURED_CONTENT,
494+
description:
495+
"Returns structured content along with an output schema for client data validation",
496+
inputSchema: zodToJsonSchema(StructuredContentSchema.input) as ToolInput,
497+
outputSchema: zodToJsonSchema(StructuredContentSchema.output) as ToolOutput,
498+
},
466499
];
467500

468501
return { tools };
@@ -652,6 +685,28 @@ export const createServer = () => {
652685
};
653686
}
654687

688+
if (name === ToolName.STRUCTURED_CONTENT) {
689+
// The same response is returned for every input.
690+
const validatedArgs = StructuredContentSchema.input.parse(args);
691+
692+
const weather = {
693+
temperature: 22.5,
694+
conditions: "Partly cloudy",
695+
humidity: 65
696+
}
697+
698+
const backwardCompatiblecontent = {
699+
type: "text",
700+
text: JSON.stringify(weather)
701+
}
702+
703+
return {
704+
content: [ backwardCompatiblecontent ],
705+
structuredContent: weather
706+
};
707+
}
708+
709+
655710
throw new Error(`Unknown tool: ${name}`);
656711
});
657712

0 commit comments

Comments
 (0)