Skip to content

Commit e1abdab

Browse files
committed
update examples, update package-lock.json
1 parent 6079cd8 commit e1abdab

8 files changed

+258
-431
lines changed

package-lock.json

Lines changed: 10 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/examples/client/parallelToolCallsClient.ts

Lines changed: 7 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -61,19 +61,13 @@ async function main(): Promise<void> {
6161
// Log the results from each tool call
6262
for (const [caller, result] of Object.entries(toolResults)) {
6363
console.log(`\n=== Tool result for ${caller} ===`);
64-
if (result.content) {
65-
result.content.forEach((item: { type: string; text?: string; }) => {
66-
if (item.type === 'text') {
67-
console.log(` ${item.text}`);
68-
} else {
69-
console.log(` ${item.type} content:`, item);
70-
}
71-
});
72-
} else if (result.structuredContent) {
73-
console.log(` Structured content: ${result.structuredContent}`);
74-
} else {
75-
console.log(` No content returned`);
76-
}
64+
result.content?.forEach((item: { type: string; text?: string; }) => {
65+
if (item.type === 'text') {
66+
console.log(` ${item.text}`);
67+
} else {
68+
console.log(` ${item.type} content:`, item);
69+
}
70+
});
7771
}
7872

7973
// 3. Wait for all notifications (10 seconds)

src/examples/client/simpleStreamableHttp.ts

Lines changed: 7 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -341,19 +341,13 @@ async function callTool(name: string, args: Record<string, unknown>): Promise<vo
341341
});
342342

343343
console.log('Tool result:');
344-
if (result.content) {
345-
result.content.forEach(item => {
346-
if (item.type === 'text') {
347-
console.log(` ${item.text}`);
348-
} else {
349-
console.log(` ${item.type} content:`, item);
350-
}
351-
});
352-
} else if (result.structuredContent) {
353-
console.log(` Structured content: ${result.structuredContent}`);
354-
} else {
355-
console.log(' No content returned');
356-
}
344+
result.content?.forEach(item => {
345+
if (item.type === 'text') {
346+
console.log(` ${item.text}`);
347+
} else {
348+
console.log(` ${item.type} content:`, item);
349+
}
350+
});
357351
} catch (error) {
358352
console.log(`Error calling tool ${name}: ${error}`);
359353
}

src/examples/client/streamableHttpWithSseFallbackClient.ts

Lines changed: 7 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -173,19 +173,13 @@ async function startNotificationTool(client: Client): Promise<void> {
173173
const result = await client.request(request, CallToolResultSchema);
174174

175175
console.log('Tool result:');
176-
if (result.content) {
177-
result.content.forEach(item => {
178-
if (item.type === 'text') {
179-
console.log(` ${item.text}`);
180-
} else {
181-
console.log(` ${item.type} content:`, item);
182-
}
183-
});
184-
} else if (result.structuredContent) {
185-
console.log(` Structured content: ${result.structuredContent}`);
186-
} else {
187-
console.log(' No content returned');
188-
}
176+
result.content?.forEach(item => {
177+
if (item.type === 'text') {
178+
console.log(` ${item.text}`);
179+
} else {
180+
console.log(` ${item.type} content:`, item);
181+
}
182+
});
189183
} catch (error) {
190184
console.log(`Error calling notification tool: ${error}`);
191185
}
Lines changed: 116 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,116 @@
1+
#!/usr/bin/env node
2+
/**
3+
* Example MCP server demonstrating tool outputSchema support using the low-level Server API
4+
* This server manually handles tool listing and invocation requests to return structured data
5+
* For a simpler high-level API approach, see mcpServerOutputSchema.ts
6+
*/
7+
8+
import { Server } from "../../server/index.js";
9+
import { StdioServerTransport } from "../../server/stdio.js";
10+
import { CallToolRequest, CallToolRequestSchema, ErrorCode, ListToolsRequestSchema, McpError } from "../../types.js";
11+
12+
const server = new Server(
13+
{
14+
name: "output-schema-low-level-example",
15+
version: "1.0.0",
16+
},
17+
{
18+
capabilities: {
19+
tools: {},
20+
},
21+
}
22+
);
23+
24+
// Tool with structured output
25+
server.setRequestHandler(ListToolsRequestSchema, async () => ({
26+
tools: [
27+
{
28+
name: "get_weather",
29+
description: "Get weather information for a city",
30+
inputSchema: {
31+
type: "object",
32+
properties: {
33+
city: { type: "string", description: "City name" },
34+
country: { type: "string", description: "Country code (e.g., US, UK)" }
35+
},
36+
required: ["city", "country"]
37+
},
38+
outputSchema: {
39+
type: "object",
40+
properties: {
41+
temperature: {
42+
type: "object",
43+
properties: {
44+
celsius: { type: "number" },
45+
fahrenheit: { type: "number" }
46+
},
47+
required: ["celsius", "fahrenheit"]
48+
},
49+
conditions: {
50+
type: "string",
51+
enum: ["sunny", "cloudy", "rainy", "stormy", "snowy"]
52+
},
53+
humidity: { type: "number", minimum: 0, maximum: 100 },
54+
wind: {
55+
type: "object",
56+
properties: {
57+
speed_kmh: { type: "number" },
58+
direction: { type: "string" }
59+
},
60+
required: ["speed_kmh", "direction"]
61+
}
62+
},
63+
required: ["temperature", "conditions", "humidity", "wind"]
64+
}
65+
}
66+
]
67+
}));
68+
69+
server.setRequestHandler(CallToolRequestSchema, async (request: CallToolRequest) => {
70+
switch (request.params.name) {
71+
case "get_weather": {
72+
const { city, country } = request.params.arguments as { city: string; country: string };
73+
74+
// Parameters are available but not used in this example
75+
void city;
76+
void country;
77+
78+
// Simulate weather API call
79+
const temp_c = Math.round((Math.random() * 35 - 5) * 10) / 10;
80+
const conditions = ["sunny", "cloudy", "rainy", "stormy", "snowy"][Math.floor(Math.random() * 5)];
81+
82+
// Return structured content matching the outputSchema
83+
return {
84+
structuredContent: {
85+
temperature: {
86+
celsius: temp_c,
87+
fahrenheit: Math.round((temp_c * 9/5 + 32) * 10) / 10
88+
},
89+
conditions,
90+
humidity: Math.round(Math.random() * 100),
91+
wind: {
92+
speed_kmh: Math.round(Math.random() * 50),
93+
direction: ["N", "NE", "E", "SE", "S", "SW", "W", "NW"][Math.floor(Math.random() * 8)]
94+
}
95+
}
96+
};
97+
}
98+
99+
default:
100+
throw new McpError(
101+
ErrorCode.MethodNotFound,
102+
`Unknown tool: ${request.params.name}`
103+
);
104+
}
105+
});
106+
107+
async function main() {
108+
const transport = new StdioServerTransport();
109+
await server.connect(transport);
110+
console.error("Low-level Output Schema Example Server running on stdio");
111+
}
112+
113+
main().catch((error) => {
114+
console.error("Server error:", error);
115+
process.exit(1);
116+
});

0 commit comments

Comments
 (0)