सरल भाषा में
MCP सर्वर एक छोटा प्रोग्राम है जिसे आप एक बार लिखते हैं। यह किसी भी क्षमता को — डेटाबेस, API, या लोकल फ़ाइल सिस्टम — रैप करता है और उसे एक मानक प्रोटोकॉल के ज़रिए किसी भी MCP-संगत AI होस्ट (Claude Desktop, Cursor, आपका अपना agent) को दे देता है। सर्वर चालू होने के बाद होस्ट आपके tools को अपने-आप खोज लेता है, और मॉडल आपको होस्ट-विशिष्ट glue कोड लिखे बिना उन्हें invoke कर सकता है।

यह लेख बनाने वाले हिस्से के बारे में है। आप तीन सर्वर primitives (tools, resources, prompts) समझेंगे, आधिकारिक Python और TypeScript SDK से उन्हें परिभाषित करना सीखेंगे, transport चुनना सीखेंगे, और होस्टेड डिप्लॉयमेंट से पहले लोकल पर टेस्ट करने का तरीका जानेंगे। अगर अभी भी साफ़ नहीं है कि MCP है क्या, तो पहले MCP क्या है? पढ़ें और फिर वापस आएं — यह लेख मानकर चलता है कि आप प्रोटोकॉल की बुनियादी बातें समझते हैं।
अपना सर्वर बनाना क्यों मायने रखता है
कम्युनिटी के बनाए सैकड़ों MCP सर्वर पहले से मौजूद हैं — GitHub, Slack, Postgres, Google Drive और अन्य के लिए। लेकिन कोई भी internal सिस्टम, proprietary API, या domain-specific tool, जिस पर आपका नियंत्रण है, उसके लिए एक सर्वर आपको लिखना होगा। एक बार लिख लिया, तो आपकी टीम का हर MCP होस्ट उस tool को बिना अतिरिक्त खर्च के इस्तेमाल कर पाता है। हर नए AI ऐप के लिए आपको इंटीग्रेशन दोबारा नहीं लिखना पड़ता।
अपना सर्वर बनाना किसी AI agent को आपके अपने संदर्भ में सच में उपयोगी बनाने का सबसे तेज़ तरीका भी है। Agent उतने ही सक्षम होते हैं जितने tools तक वे पहुँच सकते हैं। साफ़ विवरण और कसे हुए input schema वाला अच्छी तरह डिज़ाइन किया गया MCP सर्वर मॉडल को सही tool चुनने में कहीं ज़्यादा भरोसेमंद बनाता है, बजाय किसी अस्पष्ट, सब-कुछ-समेटने वाले function के।
- Internal APIs जो third-party सर्वर कवर नहीं करते — HR सिस्टम, billing डेटा, internal knowledge bases
- लोकल tools जैसे shell commands, file watchers, या dev-environment introspection, जो डेवलपर की मशीन पर ही चलने चाहिए
- Composite सर्वर जो context का झंझट कम करने के लिए कई downstream सेवाओं को एक ही MCP facade के पीछे रैप करते हैं
- Regulated या air-gapped वातावरण जहाँ डेटा आपके infrastructure से बाहर नहीं जा सकता और होस्टेड सर्वर विकल्प नहीं है
MCP सर्वर की संरचना
हर MCP सर्वर की भाषा चाहे जो हो, उसकी संरचना चार परतों वाली ही होती है: एक SDK instance जो lifecycle संभालता है, एक या अधिक primitives (tools, resources, prompts) जिन्हें आप रजिस्टर करते हैं, एक transport जो JSON-RPC संदेशों को होस्ट तक और वहाँ से लाता-ले जाता है, और आपका business logic — वह असली कोड जो काम करता है।
तीन primitives एक नज़र में
MCP होस्ट यह जानते हैं कि आपका सर्वर क्या देता है, इसके लिए वे tools/list, resources/list, और prompts/list कॉल करते हैं। आप primitives को स्टार्टअप पर एक बार रजिस्टर करते हैं; list के जवाब SDK अपने-आप संभालता है।
- टूल — वे functions जिन्हें मॉडल कोई action लेने या डेटा लाने के लिए call कर सकता है। हर tool का एक
name, इंसानों के लिए पढ़ने योग्यdescription, और एक JSON SchemainputSchemaहोता है। व्यवहार में tools सबसे ज़्यादा इस्तेमाल होने वाला primitive हैं। - संसाधन — केवल-पढ़ने-योग्य डेटा जिसे होस्ट context के रूप में लोड कर सकता है। Resource को फ़ाइल की तरह समझें: होस्ट उसे पढ़ता है, सीधे मॉडल नहीं। यह उन चीज़ों के लिए अच्छा है जैसे DB schema, config फ़ाइल, या dashboard snapshot, जिन्हें मॉडल कुछ करने से पहले समझना ज़रूरी है।
- Prompts — सर्वर द्वारा दिए गए दोबारा इस्तेमाल होने वाले prompt templates। होस्ट parameters भर सकता है और नतीजे को बातचीत में डाल सकता है। किसी codebase या dataset के बारे में टीम कैसे सवाल पूछती है, उसे मानकीकृत करने के लिए उपयोगी।
एक tool call कैसे चलता है
आपका पहला सर्वर लिखना
नीचे Python और TypeScript दोनों में न्यूनतम काम करने वाले सर्वर दिए गए हैं। दोनों एक tool (get_word_count) देते हैं जो किसी string में शब्द गिनता है। यह छोटा उदाहरण वह सारा boilerplate दिखाता है जो आपको चाहिए — इसे बड़ा करने के लिए और @mcp.tool() decorators या server.registerTool() calls जोड़ें।
Python (FastMCP)
# install the SDK (Python 3.10+)
uv add "mcp[cli]"
# or: pip install "mcp[cli]"# server.py
from mcp.server.fastmcp import FastMCP
# 1. Create the server instance (name shown in host UI)
mcp = FastMCP("text-utils")
# 2. Register a tool using the decorator
@mcp.tool()
def get_word_count(text: str) -> int:
"""Count the number of words in a block of text.
Args:
text: The text to count words in.
"""
# Type hints → JSON Schema input schema (automatic)
# Docstring → tool description the model reads (automatic)
return len(text.split())
# 3. Run over stdio so a local host can launch this as a subprocess
if __name__ == "__main__":
mcp.run(transport="stdio")FastMCP आपके Python type hints पढ़कर inputSchema के लिए JSON Schema अपने-आप बनाता है। Docstring tool का description बन जाता है। FastMCP इस्तेमाल करते समय आपको शायद ही कभी कच्चा JSON Schema हाथ से लिखना पड़ता है।
TypeScript (McpServer)
# install the SDK (Node 16+)
npm install @modelcontextprotocol/sdk zod@3
npm install -D typescript @types/node// src/index.ts
import { McpServer } from "@modelcontextprotocol/sdk/server/mcp.js";
import { StdioServerTransport } from "@modelcontextprotocol/sdk/server/stdio.js";
import { z } from "zod";
// 1. Create the server instance
const server = new McpServer({
name: "text-utils",
version: "1.0.0",
});
// 2. Register a tool — inputSchema uses Zod, which the SDK converts to JSON Schema
server.registerTool(
"get_word_count",
{
description: "Count the number of words in a block of text.",
inputSchema: {
text: z.string().describe("The text to count words in."),
},
},
async ({ text }) => ({
content: [{ type: "text", text: String(text.split(/\s+/).length) }],
})
);
// 3. Connect stdio transport and start
async function main() {
const transport = new StdioServerTransport();
await server.connect(transport);
// server is now running; do NOT use console.log() — it corrupts stdio
console.error("text-utils MCP server running on stdio");
}
main();एक resource expose करना
Resources होस्ट को tool call किए बिना डेटा को context के रूप में लोड करने देते हैं। आप उन्हें अपनी पसंद के URI scheme के साथ रजिस्टर करते हैं। यहाँ एक Python उदाहरण है जो एक स्थिर config फ़ाइल expose करता है:
@mcp.resource("config://app/settings")
def get_settings() -> str:
"""Current application settings (read-only)."""
return open("settings.json").read()एक prompt template expose करना
from mcp.server.fastmcp import FastMCP
from mcp.types import PromptMessage, TextContent
@mcp.prompt()
def summarize_code(language: str, code: str) -> list[PromptMessage]:
"""Summarize a code snippet in plain English.
Args:
language: Programming language of the snippet.
code: The source code to summarize.
"""
return [
PromptMessage(
role="user",
content=TextContent(
type="text",
text=f"Summarize the following {language} code in one paragraph:\n\n{code}",
),
)
]Transport चुनना: लोकल बनाम होस्टेड
वही सर्वर कोड दो transports पर चल सकता है। यह चुनाव तय करता है कि कौन कनेक्ट कर सकता है और सर्वर कहाँ रहता है।
- होस्ट सर्वर को subprocess के रूप में चलाता है
- संदेश stdin/stdout के ज़रिए जाते हैं
- हर process instance के लिए एक client
- कोई network नहीं, कोई auth नहीं चाहिए
- सबसे अच्छा: filesystem, git, लोकल dev tools
- Config: होस्ट JSON में command + args
- सर्वर एक सामान्य HTTP सेवा है
- स्ट्रीमिंग के लिए HTTP POST + SSE इस्तेमाल होता है
- कई clients, एक सर्वर process
- OAuth / bearer token auth चाहिए
- सबसे अच्छा: SaaS APIs, टीम में साझा tools
- Config: होस्ट JSON में URL + auth token
लोकल पर stdio के ज़रिए चलाना
stdio सर्वर को होस्ट हर बार शुरू होने पर on demand लॉन्च करता है। आप उसे एक JSON config से रजिस्टर करते हैं जो होस्ट को बताता है कि कौन सी command चलानी है। Claude Desktop के लिए यह config macOS पर ~/Library/Application Support/Claude/claude_desktop_config.json में रहता है, या Windows पर %APPDATA%\Claude\claude_desktop_config.json में:
{
"mcpServers": {
"text-utils": {
"command": "uv",
"args": [
"--directory", "/absolute/path/to/project",
"run", "server.py"
]
}
}
}HTTP पर रिमोटली चलाना
होस्टेड सर्वर के लिए FastMCP में transport को stdio से streamable-http में बदलें। SDK एक HTTP सर्वर शुरू करता है जिसे आप किसी भी cloud runtime पर deploy कर सकते हैं:
# Remote HTTP server (FastMCP)
if __name__ == "__main__":
mcp.run(
transport="streamable-http",
host="0.0.0.0",
port=8000,
path="/mcp", # endpoint the host connects to
)रिमोट सर्वर के लिए होस्ट config command की जगह URL इस्तेमाल करता है:
{
"mcpServers": {
"text-utils-remote": {
"url": "https://your-server.example.com/mcp",
"headers": {
"Authorization": "Bearer <your-token>"
}
}
}
}होस्ट से कनेक्ट करने से पहले टेस्ट करना
अपने सर्वर को Claude या किसी और होस्ट से जोड़ने से पहले इसे MCP Inspector से टेस्ट करें — यह एक browser-based tool है जो आपके सर्वर से कनेक्ट होता है, हर primitive की सूची दिखाता है, और आपको हाथ से tools call करने देता है:
# run from your project directory
npx @modelcontextprotocol/inspector uv run server.py
# or for Node/TypeScript
npx @modelcontextprotocol/inspector node build/index.jsInspector localhost:5173 (डिफ़ॉल्ट रूप से) पर एक UI खोलता है, जहाँ आप tools की सूची देख सकते हैं, arguments भर सकते हैं, और raw JSON-RPC संदेशों की जाँच कर सकते हैं। टूटे हुए schemas को पूरे agent loop के अंदर ठीक करने की बजाय यहीं ठीक करें।
आगे की जानकारी
बेहतरीन tool descriptions कैसे लिखें
Tool की गुणवत्ता ज़्यादातर description की गुणवत्ता पर निर्भर करती है। मॉडल सिर्फ़ नामों और descriptions के आधार पर चुनता है कि कौन सा tool call करना है — वह आपका कोड नहीं देख सकता। Descriptions ऐसे लिखें जैसे किसी नए टीम सदस्य को समझा रहे हों: tool क्या करता है, किन बातों का ध्यान रखना है, और कब मॉडल को किसी मिलते-जुलते tool के बजाय इसे प्राथमिकता देनी चाहिए। अगर tool के side effects हैं (लिखना, मिटाना, ईमेल भेजना) तो उसे साफ़-साफ़ बताएं — मॉडल side effects वाले tools को लेकर ज़्यादा सावधान रहते हैं जब उन्हें यह पता हो।
समृद्ध content लौटाना
एक tool handler content array लौटाता है। हर item का type text, image, या resource होता है। text type सबसे आम है; image से आप vision मॉडलों के लिए base64-encoded image डेटा लौटा सकते हैं; resource एक resource URI एम्बेड करता है जिसे होस्ट dereference कर सकता है। tool की विफलता रिपोर्ट करने का सही तरीका है text विवरण के साथ isError: true flag लौटाना — unhandled exceptions न फेंकें।
# Returning a structured error from a tool (Python)
@mcp.tool()
def divide(a: float, b: float) -> dict:
"""Divide a by b."""
if b == 0:
return {"isError": True, "content": [{"type": "text", "text": "Cannot divide by zero"}]}
return {"content": [{"type": "text", "text": str(a / b)}]}Dynamic tool रजिस्ट्रेशन
दोनों SDK सर्वर शुरू होने के बाद tools जोड़ने या हटाने को सपोर्ट करते हैं। रनटाइम पर tool जोड़ते समय होस्ट को notifications/tools/list_changed संदेश भेजकर सूचित करें — जब आप server object के ज़रिए रजिस्टर करते हैं तो SDK यह अपने-आप करता है। इससे ऐसे पैटर्न संभव होते हैं जैसे केवल प्रमाणित users के लिए premium tools चालू करना, या स्टार्टअप पर database से उपलब्ध tools लोड करना।
सुरक्षा चेकलिस्ट
- सभी inputs को validate करें — JSON Schema गायब fields और गलत types पकड़ लेता है, लेकिन बिज़नेस-लॉजिक validation (जैसे path traversal, SQL injection) आपकी ज़िम्मेदारी है।
- Credentials को सीमित रखें — सर्वर process के पास केवल वही API keys होनी चाहिए जो उसे सच में चाहिए। Environment variables इस्तेमाल करें, कभी hardcode न करें।
- ध्यान रखें कि क्या लौटाते हैं — tool का result मॉडल के context में जस का तस पहुँचता है। कच्चे secrets, PII, या बड़े blobs न लौटाएं जो context window को फुला दें।
- Remote सर्वरों पर auth इस्तेमाल करें — खुला HTTP endpoint एक सार्वजनिक RCE जोखिम है। tool calls स्वीकार करने से पहले हमेशा bearer token या OAuth flow ज़रूरी करें।
- Tool permissions का ऑडिट करें — destructive tools (delete, send, deploy) को elicitation के ज़रिए साफ़ पुष्टि मांगनी चाहिए या डिफ़ॉल्ट रूप से बंद होने चाहिए।
जब आपका सर्वर पक्का हो जाए, तो अगला कदम है कई सर्वरों को एक multi-agent system में जोड़ना, या अधिक जटिल orchestration के लिए इसे Claude Agent SDK जैसे framework के साथ जोड़ना। आज आप जो MCP सर्वर बना रहे हैं, वह उस भविष्य में भी बिना किसी बदलाव के काम करेगा — प्रोटोकॉल ही टिकाऊ हिस्सा है।
अक्सर पूछे जाने वाले प्रश्न
MCP सर्वर बनाने के लिए क्या JSON Schema जानना ज़रूरी है?
High-level SDK के साथ नहीं। FastMCP (Python) आपके type hints और docstrings पढ़कर JSON Schema अपने-आप बना देता है। TypeScript SDK Zod schemas स्वीकार करता है और उन्हें बदल देता है। आप कच्चा JSON Schema तभी लिखते हैं जब आप lower-level SDK primitives इस्तेमाल करते हैं या ऐसे schema features चाहिए जो high-level wrappers उजागर नहीं करते।
क्या आप बिना TypeScript या Python के MCP सर्वर बना सकते हैं?
हाँ। आधिकारिक MCP GitHub org TypeScript, Python, Java, Kotlin, C#, Go, PHP, Ruby, Rust और Swift के लिए SDK देता है। कोई भी भाषा जो stdio या HTTP पर JSON-RPC बोल सकती है, बिना आधिकारिक SDK के भी प्रोटोकॉल लागू कर सकती है, लेकिन SDK काफ़ी boilerplate बचा देते हैं।
MCP सर्वर को Claude Desktop से कैसे जोड़ें?
Claude Desktop की config फ़ाइल खोलें (macOS पर ~/Library/Application Support/Claude/claude_desktop_config.json, Windows पर %APPDATA%\Claude\claude_desktop_config.json)। mcpServers के अंदर एक entry जोड़ें जिसमें command और आपके सर्वर को लॉन्च करने का absolute path हो, फिर Claude Desktop रीस्टार्ट करें। चैट UI में हथौड़े वाला आइकन पुष्टि करता है कि tools पहचाने गए हैं।
MCP में tool और resource में क्या फ़र्क है?
Tool एक function है जिसे मॉडल call कर सकता है — यह arguments लेता है, code चलाता है, और result लौटाता है। Resource केवल-पढ़ने-योग्य डेटा है जिसे होस्ट context के रूप में लोड करता है, कुछ-कुछ किसी फ़ाइल को जोड़ने जैसा। जब मॉडल को कोई action लेना हो या dynamic डेटा लाना हो तो tools इस्तेमाल करें; स्थिर पृष्ठभूमि जानकारी (schemas, configs, docs) के लिए resources इस्तेमाल करें जिन्हें होस्ट बातचीत में डाल सके।
MCP सर्वर के लिए stdio इस्तेमाल करें या HTTP?
अगर सर्वर उसी मशीन पर चलता है जहाँ होस्ट है (लोकल dev tools, filesystem access, निजी scripts) तो stdio इस्तेमाल करें — यह सरल, तेज़ है और इसे auth नहीं चाहिए। जब सर्वर को network पर उपलब्ध कराना हो, टीम में साझा करना हो, या cloud सेवा के रूप में deploy करना हो, तब Streamable HTTP इस्तेमाल करें। प्रोटोकॉल और आपका tool code दोनों तरह से एक ही रहते हैं; केवल transport वाली लाइन बदलती है।
अगर MCP सर्वर कनेक्ट नहीं हो रहा तो उसे कैसे debug करें?
MCP Inspector (npx @modelcontextprotocol/inspector) से शुरू करें, जो सर्वर को होस्ट से पूरी तरह अलग करके जाँचता है। अगर Inspector कनेक्ट हो जाता है और tools की सूची दिखा देता है, तो समस्या होस्ट config में है (गलत path, गलत command, या env vars की कमी)। अगर Inspector भी विफल हो, तो जाँचें कि आपका सर्वर बिना error के शुरू होता है, कि stdio सर्वर SDK के संभालने से पहले stdout पर कुछ नहीं लिखता, और होस्ट जिस environment में सर्वर लॉन्च करता है वहाँ सभी ज़रूरी dependencies इंस्टॉल हैं।
