Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 17 additions & 1 deletion apps/sim/lib/api/mcp/__integration__/code-mode.integration.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion apps/sim/lib/api/mcp/catalog.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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.'
Expand Down
32 changes: 21 additions & 11 deletions apps/sim/lib/api/mcp/code-mode.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand All @@ -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({
Expand Down Expand Up @@ -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<CallRecord>()
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. */
Expand Down Expand Up @@ -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)
Expand All @@ -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)
Expand All @@ -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
}
}

Expand Down
19 changes: 18 additions & 1 deletion apps/sim/lib/api/mcp/dispatch.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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',
}
})

Expand Down Expand Up @@ -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') })
})
})
48 changes: 40 additions & 8 deletions apps/sim/lib/api/mcp/dispatch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,15 @@ 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'
import {
type OAuthAccessTokenOptions,
withOAuthAccessTokenAudience,
} from '@/lib/auth/oauth-access-token'
import { normalizeStringRecord } from '@/lib/core/utils/records'
import {
consumeOrCancelBody,
isPayloadSizeLimitError,
Expand Down Expand Up @@ -38,7 +39,7 @@ type V2RouteHandler = (
/** One tool call, as the read and write tools accept it. */
export interface McpOperationCall {
operation: V2McpOperationName
params?: Record<string, string>
params?: Record<string, string | number>
query?: Record<string, string | number | boolean>
body?: unknown
headers?: Record<string, string>
Expand Down Expand Up @@ -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(
Expand All @@ -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 {
Expand All @@ -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,
}
}
}
Expand All @@ -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())
Expand Down Expand Up @@ -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,
Expand All @@ -213,6 +241,10 @@ export async function dispatchMcpOperation(
): Promise<CallToolResult> {
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)
}
Loading