Lesson 1 of 30 ~15 min
Course progress
0%

Model Context Protocol (MCP) Fundamentals

Pochopte MCP - standardní protokol pro připojení Claude k externím nástrojům a datům.

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ů
githubGitHub API integrace
postgresPostgreSQL databáze
puppeteerWeb automation
slackSlack 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

  1. Omezený přístup - MCP server má přístup pouze k tomu co explicitně povolíte
  2. Sandboxing - Zvažte běh v containeru
  3. Audit logging - Logujte všechny akce
  4. 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.