Skip to content

Commit 84a8721

Browse files
feature: 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 9e28ac7 commit 84a8721

File tree

2 files changed

+63
-0
lines changed

2 files changed

+63
-0
lines changed

src/everything/README.md

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,15 @@ This MCP server attempts to exercise all the features of the MCP protocol. It is
8080
- `pets` (enum): Favorite pet
8181
- Returns: Confirmation of the elicitation demo with selection summary.
8282

83+
10. `structuredContent`
84+
- Demonstrates a tool returning structured content using the example in the specification
85+
- Provides an output schema to allow testing of client SHOULD advisory to validate the result using the schema
86+
- Inputs:
87+
- `location` (string): A location or ZIP code, mock data is returned regardless of value
88+
- Returns: a response with
89+
- `structuredContent` field conformant to the output schema
90+
- A backward compatible Text Content field, a SHOULD advisory in the specification
91+
8392
### Resources
8493

8594
The server provides 100 test resources in two formats:

src/everything/everything.ts

Lines changed: 54 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"),
@@ -93,6 +96,28 @@ const GetResourceLinksSchema = z.object({
9396
.describe("Number of resource links to return (1-10)"),
9497
});
9598

99+
const StructuredContentSchema = {
100+
input: z.object({
101+
location: z
102+
.string()
103+
.trim()
104+
.min(1)
105+
.describe("City name or zip code"),
106+
}),
107+
108+
output: z.object({
109+
temperature: z
110+
.number()
111+
.describe("Temperature in celsius"),
112+
conditions: z
113+
.string()
114+
.describe("Weather conditions description"),
115+
humidity: z
116+
.number()
117+
.describe("Humidity percentage"),
118+
})
119+
};
120+
96121
enum ToolName {
97122
ECHO = "echo",
98123
ADD = "add",
@@ -104,6 +129,7 @@ enum ToolName {
104129
GET_RESOURCE_REFERENCE = "getResourceReference",
105130
ELICITATION = "startElicitation",
106131
GET_RESOURCE_LINKS = "getResourceLinks",
132+
STRUCTURED_CONTENT = "structuredContent"
107133
}
108134

109135
enum PromptName {
@@ -503,6 +529,13 @@ export const createServer = () => {
503529
"Returns multiple resource links that reference different types of resources",
504530
inputSchema: zodToJsonSchema(GetResourceLinksSchema) as ToolInput,
505531
},
532+
{
533+
name: ToolName.STRUCTURED_CONTENT,
534+
description:
535+
"Returns structured content along with an output schema for client data validation",
536+
inputSchema: zodToJsonSchema(StructuredContentSchema.input) as ToolInput,
537+
outputSchema: zodToJsonSchema(StructuredContentSchema.output) as ToolOutput,
538+
},
506539
];
507540

508541
return { tools };
@@ -777,6 +810,27 @@ export const createServer = () => {
777810
return { content };
778811
}
779812

813+
if (name === ToolName.STRUCTURED_CONTENT) {
814+
// The same response is returned for every input.
815+
const validatedArgs = StructuredContentSchema.input.parse(args);
816+
817+
const weather = {
818+
temperature: 22.5,
819+
conditions: "Partly cloudy",
820+
humidity: 65
821+
}
822+
823+
const backwardCompatiblecontent = {
824+
type: "text",
825+
text: JSON.stringify(weather)
826+
}
827+
828+
return {
829+
content: [ backwardCompatiblecontent ],
830+
structuredContent: weather
831+
};
832+
}
833+
780834
throw new Error(`Unknown tool: ${name}`);
781835
});
782836

0 commit comments

Comments
 (0)