Add support for outputSchema and optional content fields in tools

- Add outputSchema field to Tool type and RegisteredTool interface
- Make content field optional in CallToolResult
- Update ListToolsRequestSchema handler to include outputSchema in tool list responses
- Add support for structuredContent in tool results
- Update examples to handle optional content field
- Add tests for new outputSchema and structuredContent functionality
- Update ToolCallback documentation to clarify when to use structuredContent vs content

This change enables tools to define structured output schemas and return
structured JSON content, providing better type safety and validation for
tool outputs.

🤖 Generated with [Claude Code](https://claude.ai/code)

Co-Authored-By: Claude <noreply@anthropic.com>
This commit is contained in:
Basil Hosmer 2025-05-05 16:17:57 -04:00
commit a85fee407a
No known key found for this signature in database
3 changed files with 154 additions and 24 deletions

View file

@ -61,13 +61,19 @@ async function main(): Promise<void> {
// Log the results from each tool call
for (const [caller, result] of Object.entries(toolResults)) {
console.log(`\n=== Tool result for ${caller} ===`);
result.content.forEach((item: { type: string; text?: string; }) => {
if (item.type === 'text') {
console.log(` ${item.text}`);
} else {
console.log(` ${item.type} content:`, item);
}
});
if (result.content) {
result.content.forEach((item: { type: string; text?: string; }) => {
if (item.type === 'text') {
console.log(` ${item.text}`);
} else {
console.log(` ${item.type} content:`, item);
}
});
} else if (result.structuredContent) {
console.log(` Structured content: ${result.structuredContent}`);
} else {
console.log(` No content returned`);
}
}
// 3. Wait for all notifications (10 seconds)

View file

@ -341,13 +341,19 @@ async function callTool(name: string, args: Record<string, unknown>): Promise<vo
});
console.log('Tool result:');
result.content.forEach(item => {
if (item.type === 'text') {
console.log(` ${item.text}`);
} else {
console.log(` ${item.type} content:`, item);
}
});
if (result.content) {
result.content.forEach(item => {
if (item.type === 'text') {
console.log(` ${item.text}`);
} else {
console.log(` ${item.type} content:`, item);
}
});
} else if (result.structuredContent) {
console.log(` Structured content: ${result.structuredContent}`);
} else {
console.log(' No content returned');
}
} catch (error) {
console.log(`Error calling tool ${name}: ${error}`);
}

View file

@ -173,13 +173,19 @@ async function startNotificationTool(client: Client): Promise<void> {
const result = await client.request(request, CallToolResultSchema);
console.log('Tool result:');
result.content.forEach(item => {
if (item.type === 'text') {
console.log(` ${item.text}`);
} else {
console.log(` ${item.type} content:`, item);
}
});
if (result.content) {
result.content.forEach(item => {
if (item.type === 'text') {
console.log(` ${item.text}`);
} else {
console.log(` ${item.type} content:`, item);
}
});
} else if (result.structuredContent) {
console.log(` Structured content: ${result.structuredContent}`);
} else {
console.log(' No content returned');
}
} catch (error) {
console.log(`Error calling notification tool: ${error}`);
}