Skip to main content
Blaxel supports “code mode” natively, enabling a more efficient way to execute tool calls over MCP.
For more information on “code mode” or “code execution with MCP”, refer to blog posts by Cloudflare and Anthropic.
The typical flow is:
  1. The agent receives a user prompt (e.g. “List all pets”)
  2. It calls search to discover relevant endpoints in the OpenAPI spec
  3. It calls execute with a fetch() script targeting the right endpoint
  4. Blaxel spins up a sandbox, injects credentials, and runs the code
  5. The result flows back to the agent
Under the hood:
  • The agent typically does a search first to find the right endpoint, then executes a fetch call.
  • When an agent calls search, code mode evaluates a JavaScript function against the in-memory OpenAPI specification. All $ref pointers are auto-resolved so the agent sees the complete schema.
  • When an agent calls execute, the code runs inside an auto-scaling sandbox with a 10-minute idle TTL.

Deployment

Use the following code or Blaxel CLI command to deploy an MCP server in code mode. Replace the OPENAPI_REFERENCE variable with the URL to the API specification you want to expose.
import { createFunction } from "@blaxel/core";

async function main() {
  await createFunction({
    body: {
      metadata: {
        name: "my-api-code-mode",
        displayName: "API Code Mode",
      },
      spec: {
        runtime: {
          image: "blaxel/code-mode:latest",
          memory: 2048,
          envs: [
            { name: "OPENAPI_REFERENCE", value: "https://petstore3.swagger.io/api/v3/openapi.json" },
          ],
        },
      },
    },
  });
}

main().catch((err) => {
  console.error(err);
  process.exit(1);
});
Check status:
bl get mcp my-api-code-mode
Retrieve MCP endpoint url:
bl get mcp my-api-code-mode -o json | jq -r '.[] | .metadata.url'
Once status shows DEPLOYED, any agent can use the MCP server as a tool provider.

Authentication

Code mode MCP servers auto-detects security schemes defined in the OpenAPI spec and maps them to environment variables named AUTH_<SCHEME_NAME>. For example, if the spec defines a scheme called ApiKeyAuth, define an environment variable in your deployment schema called AUTH_APIKEYAUTH with the corresponding secret:
    envs:
      - name: OPENAPI_REFERENCE
        value: https://api.example.com/openapi.json
      - name: AUTH_APIKEYAUTH
        value: ${secrets.AUTH_APIKEYAUTH}

Supported auth types

OpenAPI scheme typeEnv var usage
http / bearerAuthorization: Bearer <token>
http / basicAuthorization: Basic <base64>
apiKey (header)Custom header with the key value
apiKey (query)Appended as query parameter
oauth2 / openIdConnectAuthorization: Bearer <token>

Use Claude Agent SDK with MCP code mode on Blaxel

Build an agent that connects to a Blaxel MCP server running in code mode using Claude Agent SDK.