Model Context Protocol (MCP) je otevřený standard od Anthropic pro připojení AI modelů k externím systémům.
Co je MCP?
Tradiční přístup:
[User] → [Claude] → [Response]
↓
(omezený kontext)
S MCP:
[User] → [Claude] → [MCP Server] → [External Tools/Data]
↓ ↓
[Response] ← [Results]
Klíčové koncepty
Resources
Data která může Claude číst:
{
"uri": "file:///project/src/main.py",
"name": "Main application file",
"mimeType": "text/x-python"
}
Tools
Akce které může Claude vykonat:
{
"name": "run_tests",
"description": "Run project test suite",
"inputSchema": {
"type": "object",
"properties": {
"pattern": {"type": "string", "description": "Test file pattern"}
}
}
}
Prompts
Předpřipravené instrukce:
{
"name": "code_review",
"description": "Review code changes",
"arguments": [
{"name": "file_path", "required": true}
]
}
MCP Server setup
Jednoduchý Python MCP server
# mcp_server.py
from mcp.server import Server
from mcp.server.stdio import stdio_server
server = Server("my-tools")
@server.tool()
async def read_file(path: str) -> str:
"""Read contents of a file"""
with open(path) as f:
return f.read()
@server.tool()
async def write_file(path: str, content: str) -> str:
"""Write content to a file"""
with open(path, 'w') as f:
f.write(content)
return f"Written {len(content)} bytes to {path}"
@server.tool()
async def run_command(command: str) -> str:
"""Run a shell command"""
import subprocess
result = subprocess.run(command, shell=True, capture_output=True, text=True)
return result.stdout + result.stderr
async def main():
async with stdio_server() as (read, write):
await server.run(read, write)
if __name__ == "__main__":
import asyncio
asyncio.run(main())
Claude Desktop konfigurace
// ~/Library/Application Support/Claude/claude_desktop_config.json
{
"mcpServers": {
"my-tools": {
"command": "python",
"args": ["/path/to/mcp_server.py"]
}
}
}
MCP Architecture
┌─────────────────────────────────────────────┐
│ Claude │
└─────────────────────┬───────────────────────┘
│ MCP Protocol
▼
┌─────────────────────────────────────────────┐
│ MCP Server │
│ ┌─────────┐ ┌─────────┐ ┌─────────────┐ │
│ │Resources│ │ Tools │ │ Prompts │ │
│ └────┬────┘ └────┬────┘ └──────┬──────┘ │
└───────┼───────────┼─────────────┼──────────┘
▼ ▼ ▼
[Files] [APIs/DBs] [Templates]
Předinstalované MCP servery
Anthropic poskytuje oficiální servery:
| Server | Účel |
|---|---|
filesystem | Čtení/zápis souborů |
github | GitHub API integrace |
postgres | PostgreSQL databáze |
puppeteer | Web automation |
slack | Slack integrace |
// Příklad konfigurace
{
"mcpServers": {
"filesystem": {
"command": "npx",
"args": ["-y", "@modelcontextprotocol/server-filesystem", "/path/to/allowed/dir"]
},
"github": {
"command": "npx",
"args": ["-y", "@modelcontextprotocol/server-github"],
"env": {
"GITHUB_TOKEN": "your-token"
}
}
}
}
Security considerations
- Omezený přístup - MCP server má přístup pouze k tomu co explicitně povolíte
- Sandboxing - Zvažte běh v containeru
- Audit logging - Logujte všechny akce
- Token scoping - Minimální práva pro API tokeny
# Příklad: Omezený file access
ALLOWED_PATHS = ["/project/src", "/project/docs"]
@server.tool()
async def read_file(path: str) -> str:
# Security check
if not any(path.startswith(p) for p in ALLOWED_PATHS):
raise ValueError(f"Access denied: {path}")
with open(path) as f:
return f.read()
V další lekci se podíváme na tool calling patterns.