Understanding MCP: Introduction to the Model Context Protocol
Explanation of the Model Context Protocol that OpenClaw relies on for agent-service communication.
What is MCP?
The Model Context Protocol (MCP) is a standardized protocol that enables AI agents to interface with external services and tools. It provides a consistent way for LLMs to discover, understand, and invoke capabilities beyond their training data.
Why MCP Matters
Traditional AI integrations require custom code for each service. MCP standardizes this interaction pattern:
// Without MCP - Custom integration for each service
const gmail = new GmailClient(credentials);
const calendar = new CalendarClient(credentials);
const github = new GitHubClient(credentials);
// ... 100+ more integrations
// With MCP - Standardized interface
const services = mcp.discover();
services.forEach(s => mcp.invoke(s.name, params));
MCP Architecture
Core Components
┌─────────────────────────────────────────────────┐
│ MCP Host │
│ (OpenClaw Runtime) │
└─────────────────────┬───────────────────────────┘
│
┌────────────┼────────────┐
│ │ │
┌────▼────┐ ┌────▼────┐ ┌────▼────┐
│ Server │ │ Server │ │ Server │
│ (Gmail) │ │(GitHub) │ │(Custom) │
└─────────┘ └─────────┘ └─────────┘
Server Types
- Tool Servers: Provide callable functions (send email, create file)
- Resource Servers: Expose data for context (file contents, database records)
- Prompt Servers: Offer pre-built prompt templates
How OpenClaw Uses MCP
Service Discovery
At startup, OpenClaw queries connected MCP servers:
// Automatic service discovery
const tools = await mcp.listTools();
// Returns:
[
{ name: 'gmail_send', description: 'Send an email...' },
{ name: 'calendar_create', description: 'Create event...' },
{ name: 'github_pr_create', description: 'Create PR...' }
]
Tool Invocation
When the LLM decides to use a tool:
// LLM generates tool call
{
"tool": "gmail_send",
"arguments": {
"to": "[email protected]",
"subject": "Meeting Tomorrow",
"body": "Let's meet at 2pm..."
}
}
// OpenClaw executes via MCP
const result = await mcp.invoke('gmail_send', arguments);
// Returns: { success: true, messageId: 'abc123' }
Configuring MCP Servers
Built-in Servers
// config.js
module.exports = {
mcp: {
servers: [
{ name: 'filesystem', enabled: true },
{ name: 'shell', enabled: true },
{ name: 'browser', enabled: true }
]
}
}
External Servers
// Connect to external MCP servers
{
mcp: {
servers: [
{
name: 'gmail',
type: 'stdio',
command: 'npx',
args: ['@mcp-servers/gmail']
},
{
name: 'github',
type: 'http',
url: 'http://localhost:3001/mcp'
}
]
}
}
Security Model
Capability Restrictions
MCP servers can define capability boundaries:
{
"capabilities": {
"tools": {
"read": true, // Can read files
"write": false, // Cannot write files
"execute": false // Cannot run commands
}
}
}
Permission Prompting
Configure OpenClaw to prompt for sensitive operations:
# AGENTS.md
## Permission Requirements
- File write: Prompt user
- Email send: Require confirmation
- External API: Log and continue
Building Custom MCP Servers
Create your own MCP server for custom integrations:
// custom-server.js
const { MCPServer } = require('@mcp/server');
const server = new MCPServer({
name: 'custom-tools',
version: '1.0.0'
});
server.addTool({
name: 'custom_action',
description: 'Performs a custom action',
parameters: {
input: { type: 'string', required: true }
},
handler: async ({ input }) => {
// Your custom logic here
return { result: `Processed: ${input}` };
}
});
server.start();
Conclusion
MCP is the backbone of OpenClaw's extensibility. Understanding this protocol enables you to integrate any service, build custom capabilities, and participate in the growing ecosystem of MCP-compatible tools.