Skip to content

feat(mcp): add code mode to the Sim MCP server - #10204

Merged
waleedlatif1 merged 4 commits into
stagingfrom
waleed/mcp-code-mode
Nov 19, 2026
Merged

waleedlatif1 merged 4 commits into
stagingfrom
waleed/mcp-code-mode

Conversation

@waleedlatif1

Copy link
Copy Markdown
Collaborator

Summary

  • Add code mode to the Sim MCP server: execute_read_code and execute_write_code run a JS/TS program that calls the v2 API through sim.api.<operation>(input) and return only its value, plus a per-call trace (operation, status, durationMs) and captured logs
  • Extends the existing tools rather than replacing them (same pattern as Cloudflare's search + execute, Stainless, FastMCP); the call tools stay for single calls and clients that don't run code
  • Two tools instead of a mode argument, mirroring the read/write call tools: client approvals and readOnlyHint are per tool, so a mode arg would let an auto-approved read tool run writes. The read tool refuses write operations on the host, from the catalog's tool kind
  • Programs run as a new sim-api-code sandbox task in the existing isolated-vm pool: no bundles, no fetch (task mode never installs it), one simApi broker. Every call goes through dispatchMcpRequest with the request's own credential, so auth, scopes, workspace permissions, refusals, rate limits and per-call logs are unchanged; the credential never enters the isolate
  • describe_operation now returns a TypeScript signature rendered from the contract on demand (all 323 operations). The JSON Schema → TS emitter moved from the CLI generator into lib/api/contracts/typescript.ts and both use it; the generated CLI file is byte-identical
  • Limits: 60s default / 120s max, 200 calls, 8 in flight, 1 MB result (enforced in the isolate before it crosses), 64 KB logs; in-flight calls are aborted when the program ends. SimApiError carries the v2 envelope (status, code, message, details, requestId)
  • dispatchMcpRequest now separates a call rejected before dispatch from one that ran but answered unreadably (non-JSON or too large), so a write program's trace never reports an executed write as not run
  • Write code tool needs api:write (reuses the rawRoute gate, so read-only keys and api:read tokens get the step-up/403 before any program runs)
  • Behind the mcp-code-mode feature flag (user or workspace rollout, off by default); the flag is only evaluated for initialize, tools/list, and code-tool calls
  • Shared loadTypeScript loader now used by both code mode and function execution
  • Docs: "Code mode (beta)" section in mcp/tools.mdx

Type of Change

  • New feature

Testing

  • lib/api/mcp/__integration__/code-mode.integration.ts (9 tests, real Postgres + real isolate pool through /api/mcp): flag gating (listing and calls), write-then-paged-read through real routes, read program refused a write with no row change, read-only key refused the write tool with 403, v2 404 as a catchable SimApiError, no fetch/process/require and no credential anywhere in a deep walk of isolate globals, uncaught error returns prior calls, TS stripping + syntax error line, timeout / call cap / result cap
  • Red-checked each guard by reverting it: read-mode refusal, write-tool scope gate, call cap, in-isolate result cap each turn their test red
  • lib/api/mcp/{route-handler,dispatch,catalog}.test.ts, lib/function-execution/execute-request.test.ts, scripts/generate-v2-{cli-api,mcp-operations}.test.ts
  • generate-v2-cli-api.ts --check (unchanged output), type-check, biome, check:api-validation:strict, check:utils, check:boundaries, check:test-patterns, and other targeted audits; full check:audits in CI

Checklist

  • Code follows project style guidelines
  • Self-reviewed my changes
  • Tests added/updated and passing (new tests pass the test-audit authoring gate)
  • No new warnings introduced
  • I confirm that I have read and agree to the terms outlined in the Contributor License Agreement (CLA)

@vercel

vercel Bot commented Nov 19, 2026 •

Copy link
Copy Markdown

The latest updates on your projects. Learn more about Vercel for GitHub.

1 Skipped Deployment
Project Deployment Actions Updated
docs Skipped Skipped Nov 19, 2026 8:54am UTC

Request Review

@greptile-apps

greptile-apps Bot commented Nov 19, 2026 •

Copy link
Copy Markdown
Contributor

RetriggerView in GreptileConfidence Score: 5/5

[Critical impact] The PR appears safe to merge.

Summary

Adds optional MCP code tools that run JavaScript or TypeScript against Sim’s API in an isolated sandbox.

  • Sim MCP callers can run programs that make several API calls and return a chosen result.
  • Operation descriptions show TypeScript signatures that match the API contracts.
  • MCP programs and function execution share one lazy TypeScript loader.

Diagram

sequenceDiagram
    participant Client
    participant MCP
    participant Sandbox
    participant API
    Client->>MCP: Call read or write code tool
    MCP->>MCP: Check access and feature flag
    MCP->>Sandbox: Run program without credential
    loop Each sim.api call
        Sandbox->>MCP: Operation and input
        MCP->>MCP: Check read/write restriction and limits
        MCP->>API: Dispatch with caller credential
        API-->>Sandbox: JSON answer or error
    end
    Sandbox-->>MCP: Return value and logs
    MCP-->>Client: Result and call history
Loading

Reviews (4) · Last reviewed commit: "fix(mcp): render tuples and open objects..." · Reviewed by Greptile

Comment thread apps/sim/lib/api/mcp/code-mode.ts Resolved
Comment thread apps/sim/sandboxes/sim-api-code.ts Resolved
Comment thread apps/sim/lib/api/mcp/catalog.ts Resolved

@cubic-dev-ai cubic-dev-ai Bot left a comment •

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

1 issue found across 20 files

Confidence score: 3/5

  • In apps/sim/lib/api/contracts/typescript.ts, allOf can render A & (B | C) as A & B | C, dropping shared fields from later variants. Parenthesize members with a top-level |.
Prompt for AI agents (unresolved issues)

Check if these issues are valid — if so, understand the root cause of each and fix them. When an issue isn't valid or won't be fixed in this PR, reply in its thread with the reason and then resolve the thread. If appropriate, use sub-agents to investigate and fix each issue separately.


<file name="apps/sim/lib/api/contracts/typescript.ts">

<violation number="1" location="apps/sim/lib/api/contracts/typescript.ts:55">
P2: The `allOf` join does not parenthesize union members, so `A & (B | C)` renders as `A & B | C`, which drops the shared fields from every variant after the first. Wrap members whose rendered type contains a top-level `|` in parentheses before joining with `&`.</violation>
</file>

This PR changes authentication, authorization, or input validation. Ultrareviews find 2.4x more serious bugs than standard reviews. Comment @cubic-dev-ai ultrareview to run one.

Turn on auto-fix | Re-trigger cubic

Comment thread apps/sim/lib/api/mcp/code-mode.ts Resolved
Comment thread apps/sim/lib/api/mcp/server.ts Resolved
Comment thread apps/sim/lib/api/mcp/code-mode.ts Resolved
Comment thread apps/sim/lib/api/mcp/catalog.ts Resolved
Comment thread apps/sim/sandboxes/sim-api-code.ts Resolved
Comment thread apps/sim/sandboxes/sim-api-code.ts Resolved
@waleedlatif1

Copy link
Copy Markdown
Collaborator Author

@greptile

@waleedlatif1

Copy link
Copy Markdown
Collaborator Author

@cubic-dev-ai review this PR

@cubic-dev-ai

cubic-dev-ai Bot commented Nov 19, 2026

Copy link
Copy Markdown
Contributor

@cubic-dev-ai review this PR

@waleedlatif1 I have started the AI code review. It will take a few minutes to complete.

@cubic-dev-ai cubic-dev-ai Bot left a comment •

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

All reported issues were addressed across 22 files

Reply with feedback, questions, or to request a fix.

Turn on auto-fix | Re-trigger cubic

@waleedlatif1

Copy link
Copy Markdown
Collaborator Author

@greptile

@waleedlatif1

Copy link
Copy Markdown
Collaborator Author

@cubic-dev-ai review this PR

@cubic-dev-ai

cubic-dev-ai Bot commented Nov 19, 2026

Copy link
Copy Markdown
Contributor

@cubic-dev-ai review this PR

@waleedlatif1 I have started the AI code review. It will take a few minutes to complete.

@cubic-dev-ai cubic-dev-ai Bot left a comment •

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

All reported issues were addressed across 23 files

Reply with feedback, questions, or to request a fix.

Turn on auto-fix | Re-trigger cubic

@waleedlatif1

Copy link
Copy Markdown
Collaborator Author

@greptile

@waleedlatif1

Copy link
Copy Markdown
Collaborator Author

@cubic-dev-ai review this PR

@cubic-dev-ai

cubic-dev-ai Bot commented Nov 19, 2026

Copy link
Copy Markdown
Contributor

@cubic-dev-ai review this PR

@waleedlatif1 I have started the AI code review. It will take a few minutes to complete.

@cubic-dev-ai cubic-dev-ai Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No issues found across 23 files

Confidence score: 5/5

  • Automated review surfaced no issues in the provided summaries.
  • No files require special attention.

Turn on auto-fix | Re-trigger cubic

@waleedlatif1
waleedlatif1 merged commit 8ea9ffc into staging Nov 19, 2026
85 checks passed
@waleedlatif1
waleedlatif1 deleted the waleed/mcp-code-mode branch November 19, 2026 17:15

This branch was previously deployed

1 inactive deployment
Preview — eb9dc3ea Deployed Nov 19, 2026 by vercel[bot]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant