Chuyển đến nội dung chính

Lesson 12: Model Context Protocol (MCP) — Connection standard for Agents

What is MCP, why is standardization needed? Client/Server architecture, discovery tool, capability negotiation. Build MCP Server connecting database, GitHub API, file system.

🧠 AI & ML — Lesson 11 Lesson 12: Model Context Protocol (MCP) — Connection standard for Agent

Build AI Agents: From Zero to Production

Part 5: MCP, A2A & Multi-Agent Systems

xdev.asia

Introduction

Model Context Protocol (MCP) — developed by Anthropic — is an open standard that helps agents connect to any data source or tool in a consistent way. Instead of writing custom integration for each tool, you implement MCP Server once and all MCP-compatible clients can use it.


1. MCP Architecture

┌──────────────┐     ┌──────────────┐     ┌──────────────┐
│  MCP Client  │◄───►│  MCP Server  │◄───►│  Data Source  │
│  (Agent/IDE) │     │  (Your code) │     │  (DB/API/FS) │
└──────────────┘     └──────────────┘     └──────────────┘

2. Build MCP Server

from mcp.server import Server
from mcp.types import Tool

server = Server("my-tools")

@server.tool("query_database")
async def query_db(sql: str) -> str:
    """Execute read-only SQL query."""
    result = await db.execute(sql)
    return json.dumps(result)

@server.tool("search_github")
async def search_github(query: str, repo: str) -> str:
    """Search code in a GitHub repository."""
    ...

Summary

  • MCP = USB-C for AI — universal connection standard
  • Client/Server architecture: agents are clients, tools are servers
  • Tool discovery: the agent knows which tools are available
  • Ecosystem: 1000+ MCP servers available

Exercises

  1. Build MCP Server for SQLite database
  2. Build MCP Server for GitHub API (search repos, read files)
  3. Connect agent to 3+ MCP Servers at the same time
  4. Compare custom tool vs MCP tool: development experience