Skip to content

Commit a89234f

Browse files
feat(mcp): implement support for binary responses
1 parent 79d997e commit a89234f

22 files changed

Lines changed: 206 additions & 81 deletions

packages/mcp-server/src/dynamic-tools.ts

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import ScanDocuments from 'scan-documents';
2-
import { Endpoint } from './tools';
2+
import { Endpoint, asTextContentResult, ToolCallResult } from './tools/types';
33
import { zodToJsonSchema } from 'zod-to-json-schema';
44
import { z } from 'zod';
55
import { Cabidela } from '@cloudflare/cabidela';
@@ -41,7 +41,10 @@ export function dynamicTools(endpoints: Endpoint[]): Endpoint[] {
4141
description: 'List or search for all endpoints in the Scan Documents TypeScript API',
4242
inputSchema: zodToInputSchema(listEndpointsSchema),
4343
},
44-
handler: async (client: ScanDocuments, args: Record<string, unknown> | undefined) => {
44+
handler: async (
45+
client: ScanDocuments,
46+
args: Record<string, unknown> | undefined,
47+
): Promise<ToolCallResult> => {
4548
const query = args && listEndpointsSchema.parse(args).search_query?.trim();
4649

4750
const filteredEndpoints =
@@ -58,15 +61,15 @@ export function dynamicTools(endpoints: Endpoint[]): Endpoint[] {
5861
})
5962
: endpoints;
6063

61-
return {
64+
return asTextContentResult({
6265
tools: filteredEndpoints.map(({ tool, metadata }) => ({
6366
name: tool.name,
6467
description: tool.description,
6568
resource: metadata.resource,
6669
operation: metadata.operation,
6770
tags: metadata.tags,
6871
})),
69-
};
72+
});
7073
},
7174
};
7275

@@ -95,7 +98,7 @@ export function dynamicTools(endpoints: Endpoint[]): Endpoint[] {
9598
if (!endpoint) {
9699
throw new Error(`Endpoint ${endpointName} not found`);
97100
}
98-
return endpoint.tool;
101+
return asTextContentResult(endpoint.tool);
99102
},
100103
};
101104

@@ -120,7 +123,10 @@ export function dynamicTools(endpoints: Endpoint[]): Endpoint[] {
120123
'Invoke an endpoint in the Scan Documents TypeScript API. Note: use the `list_api_endpoints` tool to get the list of endpoints and `get_api_endpoint_schema` tool to get the schema for an endpoint.',
121124
inputSchema: zodToInputSchema(invokeEndpointSchema),
122125
},
123-
handler: async (client: ScanDocuments, args: Record<string, unknown> | undefined) => {
126+
handler: async (
127+
client: ScanDocuments,
128+
args: Record<string, unknown> | undefined,
129+
): Promise<ToolCallResult> => {
124130
if (!args) {
125131
throw new Error('No endpoint provided');
126132
}
@@ -145,7 +151,7 @@ export function dynamicTools(endpoints: Endpoint[]): Endpoint[] {
145151
throw new Error(`Invalid arguments for endpoint ${endpoint_name}:\n${error}`);
146152
}
147153

148-
return endpoint.handler(client, endpointArgs);
154+
return await endpoint.handler(client, endpointArgs);
149155
},
150156
};
151157

packages/mcp-server/src/server.ts

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -99,15 +99,7 @@ export async function executeHandler(
9999
if (options.validJson && args) {
100100
args = parseEmbeddedJSON(args, tool.inputSchema);
101101
}
102-
const result = await handler(client, args || {});
103-
return {
104-
content: [
105-
{
106-
type: 'text',
107-
text: JSON.stringify(result, null, 2),
108-
},
109-
],
110-
};
102+
return await handler(client, args || {});
111103
}
112104

113105
export const readEnv = (env: string): string | undefined => {

packages/mcp-server/src/tools/events/list-events.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
// File generated from our OpenAPI spec by Stainless. See CONTRIBUTING.md for details.
22

3+
import { asTextContentResult } from 'scan-documents-mcp/tools/types';
4+
35
import { Tool } from '@modelcontextprotocol/sdk/types.js';
46
import type { Metadata } from '../';
57
import ScanDocuments from 'scan-documents';
@@ -29,9 +31,9 @@ export const tool: Tool = {
2931
},
3032
};
3133

32-
export const handler = (client: ScanDocuments, args: Record<string, unknown> | undefined) => {
34+
export const handler = async (client: ScanDocuments, args: Record<string, unknown> | undefined) => {
3335
const body = args as any;
34-
return client.events.list(body);
36+
return asTextContentResult(await client.events.list(body));
3537
};
3638

3739
export default { metadata, tool, handler };

packages/mcp-server/src/tools/files/delete-files.ts

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
// File generated from our OpenAPI spec by Stainless. See CONTRIBUTING.md for details.
22

3+
import { asTextContentResult } from 'scan-documents-mcp/tools/types';
4+
35
import { Tool } from '@modelcontextprotocol/sdk/types.js';
46
import type { Metadata } from '../';
57
import ScanDocuments from 'scan-documents';
@@ -26,9 +28,10 @@ export const tool: Tool = {
2628
},
2729
};
2830

29-
export const handler = (client: ScanDocuments, args: Record<string, unknown> | undefined) => {
31+
export const handler = async (client: ScanDocuments, args: Record<string, unknown> | undefined) => {
3032
const { id, ...body } = args as any;
31-
return client.files.delete(id);
33+
await client.files.delete(id);
34+
return asTextContentResult('Successful tool call');
3235
};
3336

3437
export default { metadata, tool, handler };

packages/mcp-server/src/tools/files/download-files.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
// File generated from our OpenAPI spec by Stainless. See CONTRIBUTING.md for details.
22

3+
import { asBinaryContentResult } from 'scan-documents-mcp/tools/types';
4+
35
import { Tool } from '@modelcontextprotocol/sdk/types.js';
46
import type { Metadata } from '../';
57
import ScanDocuments from 'scan-documents';
@@ -26,9 +28,9 @@ export const tool: Tool = {
2628
},
2729
};
2830

29-
export const handler = (client: ScanDocuments, args: Record<string, unknown> | undefined) => {
31+
export const handler = async (client: ScanDocuments, args: Record<string, unknown> | undefined) => {
3032
const { id, ...body } = args as any;
31-
return client.files.download(id);
33+
return asBinaryContentResult(await client.files.download(id));
3234
};
3335

3436
export default { metadata, tool, handler };

packages/mcp-server/src/tools/files/list-files.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
// File generated from our OpenAPI spec by Stainless. See CONTRIBUTING.md for details.
22

3+
import { asTextContentResult } from 'scan-documents-mcp/tools/types';
4+
35
import { Tool } from '@modelcontextprotocol/sdk/types.js';
46
import type { Metadata } from '../';
57
import ScanDocuments from 'scan-documents';
@@ -31,9 +33,9 @@ export const tool: Tool = {
3133
},
3234
};
3335

34-
export const handler = (client: ScanDocuments, args: Record<string, unknown> | undefined) => {
36+
export const handler = async (client: ScanDocuments, args: Record<string, unknown> | undefined) => {
3537
const body = args as any;
36-
return client.files.list(body);
38+
return asTextContentResult(await client.files.list(body));
3739
};
3840

3941
export default { metadata, tool, handler };

packages/mcp-server/src/tools/files/retrieve-files.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
// File generated from our OpenAPI spec by Stainless. See CONTRIBUTING.md for details.
22

3+
import { asTextContentResult } from 'scan-documents-mcp/tools/types';
4+
35
import { Tool } from '@modelcontextprotocol/sdk/types.js';
46
import type { Metadata } from '../';
57
import ScanDocuments from 'scan-documents';
@@ -26,9 +28,9 @@ export const tool: Tool = {
2628
},
2729
};
2830

29-
export const handler = (client: ScanDocuments, args: Record<string, unknown> | undefined) => {
31+
export const handler = async (client: ScanDocuments, args: Record<string, unknown> | undefined) => {
3032
const { id, ...body } = args as any;
31-
return client.files.retrieve(id);
33+
return asTextContentResult(await client.files.retrieve(id));
3234
};
3335

3436
export default { metadata, tool, handler };

packages/mcp-server/src/tools/files/upload-files.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
// File generated from our OpenAPI spec by Stainless. See CONTRIBUTING.md for details.
22

3+
import { asTextContentResult } from 'scan-documents-mcp/tools/types';
4+
35
import { Tool } from '@modelcontextprotocol/sdk/types.js';
46
import type { Metadata } from '../';
57
import ScanDocuments from 'scan-documents';
@@ -31,9 +33,9 @@ export const tool: Tool = {
3133
},
3234
};
3335

34-
export const handler = (client: ScanDocuments, args: Record<string, unknown> | undefined) => {
36+
export const handler = async (client: ScanDocuments, args: Record<string, unknown> | undefined) => {
3537
const body = args as any;
36-
return client.files.upload(body);
38+
return asTextContentResult(await client.files.upload(body));
3739
};
3840

3941
export default { metadata, tool, handler };

packages/mcp-server/src/tools/image-operations/apply-effect-image-operations.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
// File generated from our OpenAPI spec by Stainless. See CONTRIBUTING.md for details.
22

3+
import { asTextContentResult } from 'scan-documents-mcp/tools/types';
4+
35
import { Tool } from '@modelcontextprotocol/sdk/types.js';
46
import type { Metadata } from '../';
57
import ScanDocuments from 'scan-documents';
@@ -36,9 +38,9 @@ export const tool: Tool = {
3638
},
3739
};
3840

39-
export const handler = (client: ScanDocuments, args: Record<string, unknown> | undefined) => {
41+
export const handler = async (client: ScanDocuments, args: Record<string, unknown> | undefined) => {
4042
const body = args as any;
41-
return client.imageOperations.applyEffect(body);
43+
return asTextContentResult(await client.imageOperations.applyEffect(body));
4244
};
4345

4446
export default { metadata, tool, handler };

packages/mcp-server/src/tools/image-operations/convert-image-operations.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
// File generated from our OpenAPI spec by Stainless. See CONTRIBUTING.md for details.
22

3+
import { asTextContentResult } from 'scan-documents-mcp/tools/types';
4+
35
import { Tool } from '@modelcontextprotocol/sdk/types.js';
46
import type { Metadata } from '../';
57
import ScanDocuments from 'scan-documents';
@@ -85,9 +87,9 @@ export const tool: Tool = {
8587
},
8688
};
8789

88-
export const handler = (client: ScanDocuments, args: Record<string, unknown> | undefined) => {
90+
export const handler = async (client: ScanDocuments, args: Record<string, unknown> | undefined) => {
8991
const body = args as any;
90-
return client.imageOperations.convert(body);
92+
return asTextContentResult(await client.imageOperations.convert(body));
9193
};
9294

9395
export default { metadata, tool, handler };

0 commit comments

Comments
 (0)