Model Context Protocols (MCP)
Connect agents to external services via Model Context Protocol servers. MCP servers expose tools and data sources that agents can use automatically.
Quick Start
class WeatherAgent < ActiveAgent::Base
generate_with :anthropic, model: "claude-haiku-4-5"
def forecast
prompt(
"What's the weather like?",
mcps: [ { name: "weather", url: "https://demo-day.mcp.cloudflare.com/sse" } ]
)
end
endProvider Support
| Provider | Support | Notes |
|---|---|---|
| OpenAI | ✅ | Via Responses API |
| Anthropic | ⚠️ | Beta |
| OpenRouter | 🚧 | In development |
| Ollama | ❌ | Not supported |
| Mock | ❌ | Not supported |
MCP Format
{
name: "server_name", # Required: server identifier
url: "https://server.url", # Required: MCP endpoint
authorization: "token" # Optional: auth token
}Single Server
class DataAgent < ActiveAgent::Base
generate_with :anthropic, model: "claude-haiku-4-5"
def analyze
prompt(
"Analyze the latest data",
mcps: [ { name: "cloudflare-demo", url: "https://demo-day.mcp.cloudflare.com/sse" } ]
)
end
endMultiple Servers
class IntegratedAgent < ActiveAgent::Base
generate_with :openai, model: "gpt-4o"
def research
prompt(
"Research the latest AI developments",
mcps: [
{ name: "cloudflare", url: "https://demo-day.mcp.cloudflare.com/sse" },
{ name: "github", url: "https://api.githubcopilot.com/mcp/", authorization: ENV["GITHUB_MCP_TOKEN"] }
]
)
end
endWith Function Tools
class HybridAgent < ActiveAgent::Base
generate_with :openai, model: "gpt-4o"
def analyze_data
prompt(
"Calculate and fetch data",
tools: [ {
name: "calculate",
description: "Perform calculations",
parameters: {
type: "object",
properties: {
operation: { type: "string" },
a: { type: "number" },
b: { type: "number" }
}
}
} ],
mcps: [ { name: "data-service", url: "https://demo-day.mcp.cloudflare.com/sse" } ]
)
end
def calculate(operation:, a:, b:)
case operation
when "add" then a + b
when "subtract" then a - b
end
end
endOpenAI
OpenAI supports MCP via the Responses API with pre-built connectors and custom servers.
Pre-built Connectors
class FileAgent < ActiveAgent::Base
generate_with :openai, model: "gpt-4o"
def search_files
prompt(
input: "Find documents about Q4 revenue",
mcps: [ { name: "dropbox", url: "mcp://dropbox" } ] # Pre-built connector
)
end
endAvailable: Dropbox, Google Drive, GitHub, Slack, and more. See OpenAI's MCP docs for the full list.
Custom Servers
class CustomAgent < ActiveAgent::Base
generate_with :openai, model: "gpt-4o"
def custom_tools
prompt(
input: "Use custom tools",
mcps: [ { name: "github_copilot", url: "https://api.githubcopilot.com/mcp/", authorization: ENV["GITHUB_MCP_TOKEN"] } ]
)
end
endAnthropic
Anthropic supports MCP servers via the mcp_servers parameter (beta). Up to 20 servers per request.
class ClaudeAgent < ActiveAgent::Base
generate_with :anthropic, model: "claude-haiku-4-5"
def use_mcp
prompt(
message: "What tools are available?",
mcps: [ { name: "demo-server", url: "https://demo-day.mcp.cloudflare.com/sse" } ]
)
end
endSee Anthropic's MCP docs for details.
Native Formats
ActiveAgent converts the common format to provider-specific formats automatically. Use native formats only if needed for provider-specific features.
class OpenAINativeAgent < ActiveAgent::Base
generate_with :openai, model: "gpt-4o"
def native_format
prompt(
input: "What can you do?",
tools: [ {
type: "mcp",
server_label: "github",
server_url: "https://api.githubcopilot.com/mcp/",
authorization: ENV["GITHUB_MCP_TOKEN"]
} ]
)
end
endclass AnthropicNativeAgent < ActiveAgent::Base
generate_with :anthropic, model: "claude-haiku-4-5"
def native_format
prompt(
message: "What can you do?",
mcp_servers: [ {
type: "url",
name: "cloudflare",
url: "https://demo-day.mcp.cloudflare.com/sse"
} ]
)
end
endTroubleshooting
Server not responding: Verify the URL is correct and accessible from your environment.
Authorization failures: Check token validity, permissions, and expiration.
Tools not available: Ensure the server implements MCP correctly and returns valid tool definitions.
Related
- Tools - Function tools and tool choice
- OpenAI Provider - OpenAI-specific features
- Anthropic Provider - Anthropic-specific features