Skip to content

Commit 6cb83f2

Browse files
fix(mcp): return tool execution error on jq failure
1 parent e6b30ad commit 6cb83f2

5 files changed

Lines changed: 46 additions & 9 deletions

File tree

packages/mcp-server/src/filtering.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,3 +12,7 @@ export async function maybeFilter(jqFilter: unknown | undefined, response: any):
1212
async function jq(json: any, jqFilter: string) {
1313
return (await initJq).json(json, jqFilter);
1414
}
15+
16+
export function isJqError(error: any): error is Error {
17+
return error instanceof Error && 'stderr' in error;
18+
}

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

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

3-
import { maybeFilter } from 'scan-documents-mcp/filtering';
4-
import { Metadata, asTextContentResult } from 'scan-documents-mcp/tools/types';
3+
import { isJqError, maybeFilter } from 'scan-documents-mcp/filtering';
4+
import { Metadata, asErrorResult, asTextContentResult } from 'scan-documents-mcp/tools/types';
55

66
import { Tool } from '@modelcontextprotocol/sdk/types.js';
77
import ScanDocuments from 'scan-documents';
@@ -46,7 +46,14 @@ export const tool: Tool = {
4646

4747
export const handler = async (client: ScanDocuments, args: Record<string, unknown> | undefined) => {
4848
const { jq_filter, ...body } = args as any;
49-
return asTextContentResult(await maybeFilter(jq_filter, await client.files.list(body)));
49+
try {
50+
return asTextContentResult(await maybeFilter(jq_filter, await client.files.list(body)));
51+
} catch (error) {
52+
if (isJqError(error)) {
53+
return asErrorResult(error.message);
54+
}
55+
throw error;
56+
}
5057
};
5158

5259
export default { metadata, tool, handler };

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

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

3-
import { maybeFilter } from 'scan-documents-mcp/filtering';
4-
import { Metadata, asTextContentResult } from 'scan-documents-mcp/tools/types';
3+
import { isJqError, maybeFilter } from 'scan-documents-mcp/filtering';
4+
import { Metadata, asErrorResult, asTextContentResult } from 'scan-documents-mcp/tools/types';
55

66
import { Tool } from '@modelcontextprotocol/sdk/types.js';
77
import ScanDocuments from 'scan-documents';
@@ -41,7 +41,14 @@ export const tool: Tool = {
4141

4242
export const handler = async (client: ScanDocuments, args: Record<string, unknown> | undefined) => {
4343
const { id, jq_filter, ...body } = args as any;
44-
return asTextContentResult(await maybeFilter(jq_filter, await client.files.retrieve(id)));
44+
try {
45+
return asTextContentResult(await maybeFilter(jq_filter, await client.files.retrieve(id)));
46+
} catch (error) {
47+
if (isJqError(error)) {
48+
return asErrorResult(error.message);
49+
}
50+
throw error;
51+
}
4552
};
4653

4754
export default { metadata, tool, handler };

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

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

3-
import { maybeFilter } from 'scan-documents-mcp/filtering';
4-
import { Metadata, asTextContentResult } from 'scan-documents-mcp/tools/types';
3+
import { isJqError, maybeFilter } from 'scan-documents-mcp/filtering';
4+
import { Metadata, asErrorResult, asTextContentResult } from 'scan-documents-mcp/tools/types';
55

66
import { Tool } from '@modelcontextprotocol/sdk/types.js';
77
import ScanDocuments from 'scan-documents';
@@ -44,7 +44,14 @@ export const tool: Tool = {
4444

4545
export const handler = async (client: ScanDocuments, args: Record<string, unknown> | undefined) => {
4646
const { jq_filter, ...body } = args as any;
47-
return asTextContentResult(await maybeFilter(jq_filter, await client.files.upload(body)));
47+
try {
48+
return asTextContentResult(await maybeFilter(jq_filter, await client.files.upload(body)));
49+
} catch (error) {
50+
if (isJqError(error)) {
51+
return asErrorResult(error.message);
52+
}
53+
throw error;
54+
}
4855
};
4956

5057
export default { metadata, tool, handler };

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

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,18 @@ export async function asBinaryContentResult(response: Response): Promise<ToolCal
8787
}
8888
}
8989

90+
export function asErrorResult(message: string): ToolCallResult {
91+
return {
92+
content: [
93+
{
94+
type: 'text',
95+
text: message,
96+
},
97+
],
98+
isError: true,
99+
};
100+
}
101+
90102
export type Metadata = {
91103
resource: string;
92104
operation: 'read' | 'write';

0 commit comments

Comments
 (0)