Lesson 164 — Advanced

MCP Server for a Real Tool

MCP (Model Context Protocol) ka matlab: ek baar server banao, aur har compatible client (chat app, coding agent) usse seedha baat kar lega. Is lesson mein tum ek real tool — calendar ya tasks — ka MCP server banate ho.

⏱ 20 min• Advanced• Multi-Agent Research Report System

Project kya hai

WHAT

Ek chhota server jo tumhare tool (Google Calendar, Notion, ek CRM, ek internal API) ko LLM clients ke liye tools mein badalta hai: list_events, create_event, search_notes — properly described, safely permissioned.

WHEN

Jab tum khud same integration har client ke liye na likhna chahte ho. MCP = USB jaisa standard: ek protocol, sab clients.

WHERE

Python (official mcp SDK) ya TypeScript. Local stdv1 se shuru, phir remote HTTP. Repo = server + config example + Inspector screenshots.

Yeh project 2026 ka integration pattern hai: agents ab custom APIs nahi, MCP servers maangte hain. Ek working server + docs = portfolio mein concrete proof ki tum protocol-level samajhte ho.

Pehla tool read-only: v1 = sirf padhne wale tools (list, get, search). Likhta tool (create, delete) tabhi jab permissions + confirmation saaf hon — write bugs irreversible hote hain.

Server banana

from mcp.server.fastmcp import FastMCP

mcp = FastMCP("my-calendar")

@mcp.tool()
def list_events(day: str) -> list[dict]:
    """Ek din ke saare events ISO date (YYYY-MM-DD) ke hisaab se list karo.
    Kaam nahi aaye toh khaali list wapas karo, guess mat karo."""
    return calendar_api.events(day)

@mcp.tool()
def search_notes(query: str) -> list[dict]:
    """Notes mein keyword search — title aur body dono."""
    return notes_index.search(query, limit=10)

if __name__ == "__main__":
    mcp.run()   # stdio transport — local clients ke liye
Description = product copy: Tool ka docstring model padhta hai. Kya karta hai, kya nahi, input format kya hai — yeh teenon likho. Vague description = model tool choose nahi karega ya galti se call karega.

Client config

{
  "mcpServers": {
    "my-calendar": {
      "command": "python",
      "args": ["/path/to/server.py"],
      "env": { "CALENDAR_TOKEN": "env-secrets-yahan" }
    }
  }
}
Config file = integration surface: README mein exact config snippet do — user copy-paste karke2 minute mein chala le. Yeh friction hi adoption decide karta hai.

Testing — Inspector se

MCP Inspector official debugging tool hai — server ko visually chalao, tools list karo, manually call karke responses dekho:

npx @modelcontextprotocol/inspector python server.py
# Khulega: http://localhost:5173
# Connect -> tools/list -> list_events call -> JSON response check
# Error path bhi chalao: galat date, khali result, token missing

Security — least privilege

Write tools par gate: create/delete tools mein confirmation ya dry-run mode rakho. Yeh interview mein "agent security" ka favourite example ban jaata hai (L8.4).

Exercise

Question: MCP ka full form? (space se, English)

Question: Naya tool banate waqt sabse safe pehla version kaisa hota hai? (hyphenated English)

Question: MCP servers ko locally debug karne wala official Meta tool? (ek shabd)

Common mistakes

Desi example: USB ka standard

Pehle har phone ka apna pin tha — USB aaya aur ek standard sabke liye ho gaya. MCP wahi standard hai AI agents ke liye: ek protocol, aur tumhara tool har compatible client mein plug-in ho jaata hai:

text
server = FastMCP("my-calendar")
server.tool("list_events")(list_events)    # read-only, pehle
server.tool("create_event")(create_event)  # permission gate ke baad
# Ek server -> har MCP client: chat app, coding agent, custom bot
Interview angle — yahan se sawaal aate hain
  • Q: MCP kya solve karta hai? (N x M integrations -> N servers + M clients, ek protocol — discovery + calling + schemas)
  • Q: Tool description kyun important hai? (model tool choose karta hai description se — selection hi UX hai)
  • Q: MCP server mein prompt injection ka surface kya hai? (tool output untrusted; tool args bhi — least privilege + audit)
  • Q: Stdio aur HTTP transport kab use karoge? (local single-user = stdio; shared/remote = HTTP + auth)
Lesson complete?

Server, config, Inspector test aur security checklist — baaki sirf real tool ka token daal kar deploy. README mein config snippet zaroor chhodo.

Popular courses: Data Science Course in Lucknow · AI Course in Lucknow · Python Course in Lucknow · Power BI Course · Data Analytics Course