Skip to content

Commit 603fba0

Browse files
authored
fix(mcp): return structured content for tools with output schemas (#97)
1 parent acc95a3 commit 603fba0

2 files changed

Lines changed: 13 additions & 1 deletion

File tree

packages/devframe/src/adapters/mcp/__tests__/mcp-server.test.ts

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,19 +61,26 @@ describe('mcp adapter (in-memory)', () => {
6161
}
6262
})
6363

64-
it('calls a tool and returns the text content', async () => {
64+
it('returns text and structured content for a tool with an output schema', async () => {
6565
const { ctx, client, cleanup } = await bootPair()
6666
try {
6767
ctx.agent.registerTool({
6868
id: 'echo',
6969
description: 'Echo.',
70+
outputSchema: {
71+
type: 'object',
72+
properties: { echoed: { type: 'object' } },
73+
required: ['echoed'],
74+
},
7075
handler: args => ({ echoed: args }),
7176
})
7277

78+
await client.listTools()
7379
const result = await client.callTool({ name: 'echo', arguments: { foo: 'bar' } })
7480
const content = result.content as Array<{ type: string, text: string }>
7581
expect(content[0]!.type).toBe('text')
7682
expect(JSON.parse(content[0]!.text)).toEqual({ echoed: { foo: 'bar' } })
83+
expect(result.structuredContent).toEqual({ echoed: { foo: 'bar' } })
7784
}
7885
finally {
7986
await cleanup()

packages/devframe/src/adapters/mcp/build-server.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -154,6 +154,10 @@ function registerToolHandlers(server: Server, ctx: DevframeNodeContext): void {
154154
server.setRequestHandler(CallToolRequestSchema, async (request) => {
155155
const { name, arguments: args } = request.params
156156
try {
157+
const tool = ctx.agent.getTool(name)
158+
const outputSchema = tool
159+
? tool.outputSchema ?? computeOutputSchema(tool, ctx)
160+
: undefined
157161
const result = await ctx.agent.invoke(name, args ?? {})
158162
return {
159163
content: [
@@ -162,6 +166,7 @@ function registerToolHandlers(server: Server, ctx: DevframeNodeContext): void {
162166
text: stringifyForMcp(result),
163167
},
164168
],
169+
...(outputSchema ? { structuredContent: result as Record<string, unknown> } : {}),
165170
}
166171
}
167172
catch (error) {

0 commit comments

Comments
 (0)