diff --git a/apps/sim/lib/api/mcp/__integration__/code-mode.integration.ts b/apps/sim/lib/api/mcp/__integration__/code-mode.integration.ts index b20b523f9b9..e341a3a3733 100644 --- a/apps/sim/lib/api/mcp/__integration__/code-mode.integration.ts +++ b/apps/sim/lib/api/mcp/__integration__/code-mode.integration.ts @@ -272,6 +272,20 @@ describe('Sim MCP code mode in PostgreSQL and the isolate pool', () => { }) }) + it('passes a numeric path parameter, as signatures declare it, through to its route', async () => { + const result = await runCode( + 'execute_read_code', + ` + try { + await sim.api.getFileVersion({ params: { fileId: 'wf_${generateId().replaceAll('-', '')}', version: 1 }, query: { workspaceId: ${JSON.stringify(workspaceId)} } }) + } catch (error) { + return { status: error.status, code: error.code } + } + ` + ) + expect(result.structuredContent?.result).toEqual({ status: 404, code: 'NOT_FOUND' }) + }) + it('gives the program no network and no credential', async () => { const result = await runCode( 'execute_read_code', @@ -320,7 +334,9 @@ describe('Sim MCP code mode in PostgreSQL and the isolate pool', () => { expect(failed.error.message).toBe('stopped while writes were queued') await sleep(3000) const created = (await tableNames()).filter((name) => name.startsWith(prefix)) - expect(created.length).toBeLessThan(40) + const sent = failed.calls.filter((call) => call.refused === undefined) + expect(sent.length).toBeLessThan(failed.calls.length) + expect(created.length).toBeLessThanOrEqual(sent.length) for (const call of failed.calls) { expect( call.status !== undefined || call.refused !== undefined || call.unreadable !== undefined diff --git a/apps/sim/lib/api/mcp/catalog.ts b/apps/sim/lib/api/mcp/catalog.ts index 3b2957725b2..b19bd82f19f 100644 --- a/apps/sim/lib/api/mcp/catalog.ts +++ b/apps/sim/lib/api/mcp/catalog.ts @@ -35,7 +35,7 @@ const REQUEST_SLOTS = ['params', 'query', 'body', 'headers'] as const export const operationRequestInput = z .object({ params: z - .record(z.string(), z.string()) + .record(z.string(), z.union([z.string(), z.number()])) .optional() .describe( 'Values for the bracketed segments of the operation’s path, by name, e.g. { "tableId": "..." }. IDs come from list operations such as listTables.' diff --git a/apps/sim/lib/api/mcp/code-mode.ts b/apps/sim/lib/api/mcp/code-mode.ts index d06eb12206c..dc50d27337a 100644 --- a/apps/sim/lib/api/mcp/code-mode.ts +++ b/apps/sim/lib/api/mcp/code-mode.ts @@ -12,7 +12,11 @@ import { operationRequestInput, resolveOperation, } from '@/lib/api/mcp/catalog' -import { dispatchMcpRequest, type McpDispatchContext } from '@/lib/api/mcp/dispatch' +import { + dispatchMcpRequest, + type McpDispatchContext, + unreadableMessage, +} from '@/lib/api/mcp/dispatch' import { createConcurrencyLimit } from '@/lib/core/utils/concurrency' import { runSandboxTask, SandboxUserCodeError } from '@/lib/execution/sandbox/run-task' import { loadTypeScript } from '@/lib/execution/typescript' @@ -27,6 +31,7 @@ const MAX_CALLS_PER_RUN = 200 /** Calls one program may have in flight at once, so a `Promise.all` cannot fan out a whole page of requests. */ const MAX_CONCURRENT_CALLS = 8 const MAX_CODE_CHARS = 100_000 +const NOT_SENT = 'The program ended before this call was sent.' export const codeModeInput = z .object({ @@ -164,6 +169,8 @@ export async function runCodeMode( const startedAt = performance.now() const runId = generateShortId(12) const calls: CallRecord[] = [] + /** Calls handed to the dispatcher; the rest of an unfinished run's calls were never sent. */ + const sent = new Set() let writes = 0 const limit = createConcurrencyLimit(MAX_CONCURRENT_CALLS) /** Ends the run's in-flight calls once the program is over, whether it returned, threw, or timed out. */ @@ -223,14 +230,14 @@ export async function runCodeMode( ) } if (!isRead) writes++ - const outcome = await limit(async () => - signal.aborted - ? { rejected: 'The program ended before this call was sent.' } - : dispatchMcpRequest( - { ...parsed.data.input, operation: resolved.operation }, - { ...context, signal, logMetadata: { codeRunId: runId, callIndex } } - ) - ) + const outcome = await limit(async () => { + if (signal.aborted) return { rejected: NOT_SENT } + sent.add(record) + return dispatchMcpRequest( + { ...parsed.data.input, operation: resolved.operation }, + { ...context, signal, logMetadata: { codeRunId: runId, callIndex } } + ) + }) if ('rejected' in outcome) { record.refused = outcome.rejected return apiFailure(400, 'INVALID_CALL', outcome.rejected) @@ -241,7 +248,7 @@ export async function runCodeMode( return apiFailure( outcome.status, 'UNREADABLE_RESPONSE', - `${resolved.operation} ran (HTTP ${outcome.status}), but its answer cannot be returned: ${outcome.unreadable} Do not repeat it; read the resource to see its state.` + unreadableMessage(resolved.operation, outcome, isRead) ) } return outcome.ok ? { dataJson: outcome.text } : envelopeFailure(outcome.status, outcome.text) @@ -268,8 +275,11 @@ export async function runCodeMode( } runEnded.abort() for (const record of calls) { - if (record.status === undefined && record.refused === undefined) { + if (record.status !== undefined || record.refused !== undefined) continue + if (sent.has(record)) { record.unreadable = 'The program ended before this call answered; it may still complete.' + } else { + record.refused = NOT_SENT } } diff --git a/apps/sim/lib/api/mcp/dispatch.test.ts b/apps/sim/lib/api/mcp/dispatch.test.ts index 02b0ade9e0c..a6c8d901793 100644 --- a/apps/sim/lib/api/mcp/dispatch.test.ts +++ b/apps/sim/lib/api/mcp/dispatch.test.ts @@ -28,6 +28,7 @@ vi.mock('@/lib/api/mcp/catalog', () => { handler: async () => mocks.route, }), callerHeaderNames: (name: string) => (name === 'completeFileUpload' ? ['upload-token'] : []), + isReadOperation: async (name: string) => name === 'getTableRow', } }) @@ -168,6 +169,22 @@ describe('dispatchMcpOperation', () => { context ) expect(result.isError).toBe(true) - expect(result.content[0]).toMatchObject({ text: expect.stringContaining('too large') }) + expect(result.content[0]).toMatchObject({ text: expect.stringContaining('Request less') }) + }) + + it('never asks for a write that answered too large to be retried', async () => { + mocks.route.mockResolvedValue(jsonResponse({ data: 'x'.repeat(1024 * 1024) })) + const result = await dispatchMcpOperation( + { + operation: 'completeFileUpload', + params: { uploadId: 'u' }, + body: { workspaceId: 'w' }, + headers: { 'upload-token': 't' }, + }, + context + ) + expect(result.isError).toBe(true) + expect(result.content[0]).toMatchObject({ text: expect.stringContaining('Do not repeat it') }) + expect(result.content[0]).not.toMatchObject({ text: expect.stringContaining('Request less') }) }) }) diff --git a/apps/sim/lib/api/mcp/dispatch.ts b/apps/sim/lib/api/mcp/dispatch.ts index 24aa23c0a59..1b8c9418527 100644 --- a/apps/sim/lib/api/mcp/dispatch.ts +++ b/apps/sim/lib/api/mcp/dispatch.ts @@ -3,7 +3,7 @@ import { createLogger } from '@sim/logger' import { CLIENT_INFO_HEADER } from '@sim/utils/client-info' import { isPlainRecord } from '@sim/utils/object' import { NextRequest } from 'next/server' -import { callerHeaderNames, getMcpOperation } from '@/lib/api/mcp/catalog' +import { callerHeaderNames, getMcpOperation, isReadOperation } from '@/lib/api/mcp/catalog' import type { V2McpOperationName } from '@/lib/api/mcp/generated/v2-operations' import { API_KEY_HEADER } from '@/lib/api/server/credential-headers' import type { V2CredentialHeaders } from '@/lib/api/server/routes/v2-credential-headers' @@ -11,6 +11,7 @@ import { type OAuthAccessTokenOptions, withOAuthAccessTokenAudience, } from '@/lib/auth/oauth-access-token' +import { normalizeStringRecord } from '@/lib/core/utils/records' import { consumeOrCancelBody, isPayloadSizeLimitError, @@ -38,7 +39,7 @@ type V2RouteHandler = ( /** One tool call, as the read and write tools accept it. */ export interface McpOperationCall { operation: V2McpOperationName - params?: Record + params?: Record query?: Record body?: unknown headers?: Record @@ -115,7 +116,31 @@ function buildHeaders( type McpDispatchOutcome = | { rejected: string } | { status: number; ok: boolean; text: string } - | { status: number; unreadable: string } + | UnreadableAnswer + +/** An answer the route gave that cannot be returned, and why. */ +interface UnreadableAnswer { + status: number + unreadable: string + tooLarge: boolean +} + +/** + * Explains an unreadable answer to the model. The operation ran either way, so a + * write is never to be repeated; only a read that came back too large can be + * retried, asking for less. + */ +export function unreadableMessage( + operation: V2McpOperationName, + answer: UnreadableAnswer, + isRead: boolean +): string { + const message = `${operation} ran (HTTP ${answer.status}), but its answer cannot be returned. ${answer.unreadable}` + if (!isRead) return `${message} Do not repeat it; read the resource to see its state.` + return answer.tooLarge + ? `${message} Request less, such as a smaller page with limit or cursor.` + : message +} /** Reads the route's JSON answer, bounded by {@link MAX_RESULT_BYTES}. */ async function readJsonAnswer( @@ -126,8 +151,9 @@ async function readJsonAnswer( if (!/[/+]json\b/i.test(contentType)) { await consumeOrCancelBody(response) return { - unreadable: `${operation} answered with ${contentType || 'an empty body'} (HTTP ${response.status}). Only JSON responses can be returned over MCP.`, status: response.status, + unreadable: `The answer is ${contentType || 'empty'}; only JSON can be returned over MCP.`, + tooLarge: false, } } try { @@ -139,8 +165,9 @@ async function readJsonAnswer( } catch (error) { if (!isPayloadSizeLimitError(error)) throw error return { - unreadable: 'Result is too large. Request a smaller page with limit or cursor.', status: response.status, + unreadable: 'The answer is too large to return.', + tooLarge: true, } } } @@ -162,7 +189,8 @@ export async function dispatchMcpRequest( const startedAt = performance.now() const { contract, handler } = getMcpOperation(call.operation) - const resolved = resolvePath(contract.path, call.params ?? {}) + const params = normalizeStringRecord(call.params) + const resolved = resolvePath(contract.path, params) if ('error' in resolved) return { rejected: resolved.error } const url = new URL(resolved.path, getBaseUrl()) @@ -195,7 +223,7 @@ export async function dispatchMcpRequest( signal: context.signal, }) const response = await withOAuthAccessTokenAudience(context.audience, () => - route(request, { params: Promise.resolve(call.params ?? {}) }) + route(request, { params: Promise.resolve(params) }) ) logger.info('Sim MCP operation dispatched', { operation: call.operation, @@ -213,6 +241,10 @@ export async function dispatchMcpOperation( ): Promise { const outcome = await dispatchMcpRequest(call, context) if ('rejected' in outcome) return toolError(outcome.rejected) - if ('unreadable' in outcome) return toolError(outcome.unreadable) + if ('unreadable' in outcome) { + return toolError( + unreadableMessage(call.operation, outcome, await isReadOperation(call.operation)) + ) + } return outcome.ok ? { content: [{ type: 'text', text: outcome.text }] } : toolError(outcome.text) }