Use this file to discover all available pages before exploring further.
Blaxel supports “code mode” natively, enabling a more efficient way to execute tool calls over MCP. Essentially, code mode turns any OpenAPI specification into a two-tool MCP server: search lets an AI agent explore the API spec, and execute runs JavaScript code in a sandbox to call the actual API.This tutorial demonstrates by deploying an API as an MCP server on Blaxel and using an agent to work with it. It uses the Claude Agent SDK, but you could also use other frameworks like LangChain, Vercel AI SDK, Mastra, or your own custom code.
For more information on “code mode” or “code execution with MCP”, refer to blog posts by Cloudflare and Anthropic.
This tutorial creates and deploys an MCP server for the example Petstore v2 API.In your project directory, create and run a file named setup.ts (TypeScript) or setup.py (Python) with the following code, or use the Blaxel CLI command shown, to deploy the API on Blaxel.
In your project directory, create a file named index.ts (TypeScript) or main.py (Python) with the following code. Replace the placeholder with the MCP server URL from the previous command.
import { query } from "@anthropic-ai/claude-agent-sdk";const blApiKey = process.env.BL_API_KEY;const anthropicApiKey = process.env.ANTHROPIC_API_KEY;const prompt = process.argv[2];if (!blApiKey) { console.error("Error: BL_API_KEY environment variable is not set"); process.exit(1);}if (!anthropicApiKey) { console.error("Error: ANTHROPIC_API_KEY environment variable is not set"); process.exit(1);}if (!prompt) { console.error("Error: prompt argument is required"); process.exit(1);}for await (const message of query({ prompt: prompt, options: { mcpServers: { petstore: { type: "http", url: "<MCP-SERVER-URL>/mcp", headers: { "Authorization": `Bearer ${blApiKey}` }, }, }, allowedTools: ["mcp__petstore__*"], },})) { if (message.type === "system" && message.subtype === "init") { for (const s of message.mcp_servers) { console.log(`MCP server "${s.name}": ${s.status}`); } } if (message.type === "assistant") { for (const block of message.message.content) { if (block.type === "tool_use") { console.log(`\n--- ${block.name} ---`); console.log(` input: ${JSON.stringify(block.input).slice(0, 200)}`); } if (block.type === "text") { console.log(` text: ${block.text.slice(0, 300)}${block.text.length > 300 ? "…" : ""}`); } } } if (message.type === "result" && message.subtype === "success") { console.log(`\n${message.result}`); } if (message.type === "result" && message.subtype !== "success") { console.error("Error:", message); }}
You should see the agent using the two available tools, first searching for the correct API endpoint and then writing and executing code to query the API and respond to your prompt.In case of errors, you can view the logs of the deployed MCP server with the command below:
bl logs mcp petstore-code-mode
The following resources will help you go further:
Build and deploy an agent on Blaxel with Claude Agent SDK
Complete tutorial for building an agent with Claude Agent SDK and deploying it on Blaxel as a serverless auto-scalable API.
Give compute to your agent with the TypeScript SDK
Complete tutorial for using the TypeScript SDK to develop an agent using Blaxel services.
Give compute to your agent with the Python SDK
Complete tutorial for using the Python SDK to develop an agent using Blaxel services.
Deploy your agent code to Blaxel
Complete tutorial for deploying AI agents to Blaxel.