OpenClaw MCP Server Guide: Connect Any API in Under 10 Minutes
The Model Context Protocol (MCP) is the secret sauce that makes OpenClaw truly extensible. While skills provide procedural knowledge, MCP servers provide live connectivityto your databases, APIs, and local filesystems.
I've spent the last few months building dozens of MCP servers for everything from Stripe billing reconciliation to custom Jira automation. The good news? You don't need to be a systems architect to build one. In fact, if you can write a basic TypeScript or Python function, you can build an MCP server in under 10 minutes.
Why MCP Over Standard API Calls?
You might ask: "Mira, why not just have the agent call the API directly using a bash script?"
The answer is standardization and discovery. When you use MCP:
- Schema is implicit: The agent automatically knows exactly what arguments a tool needs.
- Error handling is baked-in: MCP handles the transport layer, so you only focus on the logic.
- Zero-config reuse: Once an MCP server is running, any OpenClaw session (or other MCP-compatible client) can use it immediately.
Step 1: Setup Your Environment
For this guide, we'll use TypeScript and the official @modelcontextprotocol/sdk. It's the most robust way to build servers today.
mkdir my-mcp-server
cd my-mcp-server
npm init -y
npm install @modelcontextprotocol/sdk
npm install -D typescript @types/node ts-nodeCreate a tsconfig.json to ensure everything compiles correctly:
{
"compilerOptions": {
"target": "ES2022",
"module": "NodeNext",
"moduleResolution": "NodeNext",
"outDir": "build",
"strict": true
}
}Step 2: Building the Server Logic
Let's build a "Weather & Time" server. It's a classic example, but we'll make it production-ready. Create src/index.ts:
import { Server } from "@modelcontextprotocol/sdk/server/index.js";
import { StdioServerTransport } from "@modelcontextprotocol/sdk/server/stdio.js";
import {
CallToolRequestSchema,
ListToolsRequestSchema,
} from "@modelcontextprotocol/sdk/types.js";
const server = new Server(
{
name: "example-server",
version: "1.0.0",
},
{
capabilities: {
tools: {},
},
}
);
// Define available tools
server.setRequestHandler(ListToolsRequestSchema, async () => {
return {
tools: [
{
name: "get_local_time",
description: "Get the current time for a specific timezone",
inputSchema: {
type: "object",
properties: {
timezone: {
type: "string",
description: "IANA timezone string (e.g. America/Los_Angeles)",
},
},
required: ["timezone"],
},
},
],
};
});
// Implement tool logic
server.setRequestHandler(CallToolRequestSchema, async (request) => {
if (request.params.name === "get_local_time") {
const { timezone } = request.params.arguments as { timezone: string };
const time = new Date().toLocaleString("en-US", { timeZone: timezone });
return {
content: [{ type: "text", text: `The current time in ${timezone} is ${time}` }],
};
}
throw new Error("Tool not found");
});
async function main() {
const transport = new StdioServerTransport();
await server.connect(transport);
}
main().catch(console.error);Step 3: Connecting to OpenClaw
Now that your server is ready, you need to tell OpenClaw where to find it. Open your openclaw.json (usually in ~/.openclaw/) and add your server to the mcpServers block:
{
"mcpServers": {
"my-weather-server": {
"command": "ts-node",
"args": ["/absolute/path/to/my-mcp-server/src/index.ts"],
"env": {
"MY_API_KEY": "your-secret-here"
}
}
}
}Pro-tip: Always use absolute paths. Relative paths in openclaw.jsoncan lead to "file not found" errors depending on where the gateway was started.
Advanced Tips: Handling State & Auth
Most real-world APIs require authentication. Instead of hardcoding keys, leverage the env block in your configuration.
When building for production, I always recommend:
- Input Validation: Use a library like
zodto validaterequest.params.argumentsbefore processing. - Logging: Send logs to
stderr, notstdout. MCP usesstdoutfor the protocol communication; printing to it will crash the connection. - Timeouts: Implement a timeout for external API calls so your agent doesn't hang indefinitely.
Ready to build?
Get the OpenClaw Starter Kit — config templates, 5 production-ready skills, deployment checklist. Go from zero to running in under an hour.
$14 $6.99
Get the Starter Kit →Also in the OpenClaw store
Frequently Asked Questions
Can I run MCP servers in Python?
Yes! There is an official Python SDK. Use the same patterns but with the mcp library available via pip.
How do I debug an MCP server?
Check the OpenClaw gateway logs. If the server fails to start, it usually prints the stack trace to stderr, which OpenClaw captures.
Does MCP support streaming?
MCP itself is request-response for tools, but the results can be processed and streamed by the LLM back to you.
Can one server provide multiple tools?
Absolutely. You can define as many tools as you want in the ListToolsRequestSchema handler.
What is the difference between a Skill and an MCP Server?
Skills are for reasoning and instructions (how to do things). MCP Servers are for execution and data (actually doing things or fetching data).
Next Steps
Once you've mastered local servers, check out our guide onbuilding custom skills to wrap your MCP tools in high-level reasoning patterns. You might also find ourintegration guidehelpful for more complex multi-server setups.
Get the free OpenClaw quickstart guide
Step-by-step setup. Plain English. No jargon.