सरल भाषा में
एक MCP server एक छोटा, लंबे समय तक चलने वाला process है, जो किसी क्षमता को — जैसे कोई गणना, डेटाबेस query या file operation — लपेटता है और उसे मानक JSON-RPC protocol के ज़रिए किसी भी AI host (Claude Desktop, Cursor, या आपका अपना agent loop) को उपलब्ध कराता है। इसे USB-C adapter की तरह समझें: आपकी क्षमता device है, MCP protocol port है, और AI host laptop है। एक बार adapter मिल जाए, तो laptop बिना किसी custom driver के device का इस्तेमाल कर सकता है।

यह लेख एक hands-on walkthrough है। आप आधिकारिक SDK इंस्टॉल करेंगे, एक असली tool function लिखेंगे, उसे stdio पर लोकल चलाएंगे, MCP Inspector से टेस्ट करेंगे और Claude Desktop से जोड़ेंगे — यह सब लगभग 30 मिनट में। अंत तक आपके पास एक ऐसा template होगा जिसे आप अपने business logic के साथ बढ़ा सकते हैं। Python (FastMCP के साथ) और TypeScript (McpServer के साथ) दोनों कवर किए गए हैं; जो भाषा पसंद हो, उसे फॉलो करें।
अपना server बनाने की वजह
GitHub, Postgres, Google Drive, Slack और अन्य लोकप्रिय सेवाओं के लिए सैकड़ों community MCP servers पहले से मौजूद हैं। जैसे ही आपको AI को अपने खुद के सिस्टम से जोड़ना हो — कोई internal API, एक proprietary database, या domain-specific गणना — तो आपको अपना server लिखना पड़ेगा। फायदा बड़ा है: इसे एक बार लिखें, और आपकी टीम का हर MCP-compatible AI host उस क्षमता को अपने-आप पा लेगा, बिना किसी अतिरिक्त integration काम के।
अपना server बनाने की दूसरी वजह है: नियंत्रण। Community servers तीसरे पक्ष द्वारा maintain होते हैं, आपका डेटा log कर सकते हैं, और किसी और के schedule पर अपडेट होते हैं। जो server आपका अपना है, वह आपके infrastructure में चलता है, आपके credentials इस्तेमाल करता है, और ठीक वही करता है जो आप तय करते हैं।
- Internal APIs — HR systems, billing data, CRMs और internal knowledge bases, जिन्हें कोई public server कवर नहीं करता
- Local tooling — shell commands, file watchers, build system integration और dev-environment introspection
- Regulated या air-gapped environments — ऐसा डेटा जो आपके network से बाहर नहीं जाना चाहिए
- Custom logic — गणनाएं, transformations और validations जो आपके domain के लिए खास हैं और कोई generic server उन्हें व्यक्त नहीं कर सकता
MCP server शुरू से अंत तक कैसे काम करता है
कोड लिखने से पहले पूरी तस्वीर देखना मददगार है। जब AI host शुरू होता है, तो वह आपके server को subprocess के रूप में लॉन्च करता है (stdio के लिए) या HTTP से कनेक्ट होता है। दोनों पक्ष capability handshake करते हैं, फिर host आपके server से पूछता है कि वह कौन-से tools देता है। इसके बाद, जब भी model किसी tool को call करने का फैसला करता है, host कॉल आपके server तक भेजता है, आपका handler चलता है, और नतीजा model तक वापस जाता है।
SDK आपके लिए चरण 1-4 संभालता है। आपको केवल चरण 6 में handler लिखना है। आपके tool का name और description वही हैं जो model पढ़ता है, यह तय करने के लिए कि उसे call करना है या नहीं — description को एक mini-prompt की तरह समझें।
FastMCP के साथ Python walkthrough
FastMCP वह high-level layer है जो आधिकारिक mcp Python package में शामिल है। यह एक सादे Python function को एक single decorator से पूरी तरह वर्णित MCP tool में बदल देता है — JSON Schema हाथ से लिखने की ज़रूरत नहीं। यह package PyPI पर है और Anthropic द्वारा maintain किया जाता है।
चरण 1: SDK इंस्टॉल करें
# With uv (recommended — creates a virtual env automatically)
uv init my-mcp-server
cd my-mcp-server
uv add "mcp[cli]"
# Or with pip
pip install "mcp[cli]"चरण 2: server लिखें
server.py नाम की एक file बनाएं। नीचे का example एक tool expose करता है: fetch_word_count, जो किसी string में शब्द गिनता है। यह जानबूझकर न्यूनतम रखा गया है — जब आप तैयार हों, तो function body को अपने logic से बदल दें।
# server.py
from mcp.server.fastmcp import FastMCP
# Name is shown in the host's tools UI
mcp = FastMCP("text-utils")
@mcp.tool()
def fetch_word_count(text: str) -> int:
"""Count the number of words in a block of text.
Use this when the user asks how many words are in a passage,
document excerpt, or any string they provide.
Args:
text: The text whose words should be counted.
"""
# FastMCP reads the type hint (str -> int) to build the JSON Schema.
# The docstring becomes the description the model sees.
return len(text.split())
if __name__ == "__main__":
# transport="stdio" is the default; explicit here for clarity
mcp.run(transport="stdio")चरण 3: MCP Inspector से टेस्ट करें
किसी असली AI host को जोड़ने से पहले, अपने server को MCP Inspector से टेस्ट करें — यह एक browser-based tool है जो आपके server से कनेक्ट होता है, उसके tools की सूची दिखाता है, और बिना किसी Claude configuration के आपको उन्हें invoke करने देता है। Schema की गलतियां पकड़ने का यह सबसे तेज़ तरीका है।
# Run the Inspector and pass your server as the target
npx @modelcontextprotocol/inspector uv run server.pyInspector http://localhost:6274 पर खुलता है। टूल पर क्लिक करें, fetch_word_count ढूंढें, एक test string डालें, और Run Tool पर क्लिक करें। आपको नतीजे के साथ raw JSON-RPC request और response दिखेगा। यहीं गलतियां ठीक करें — यह पूरे agent session के अंदर debug करने से कहीं तेज़ है।
चरण 4: Claude Desktop से जोड़ें
Claude Desktop की config file खोलें और अपने server को mcpServers के नीचे जोड़ें। यह file 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/my-mcp-server",
"run",
"server.py"
]
}
}
}Claude Desktop को पूरी तरह बंद करके फिर से खोलें। चैट input bar में एक hammer icon दिखे तो पुष्टि है कि tools मिल गए हैं। Claude से पूछें "How many words are in this paragraph?" और कुछ text paste करें — उसे अपने-आप fetch_word_count call करना चाहिए।
McpServer के साथ TypeScript walkthrough
आधिकारिक TypeScript SDK (@modelcontextprotocol/sdk, 2026 के मध्य तक version 1.11.x) मुख्य class के रूप में McpServer और input schema validation के लिए Zod इस्तेमाल करता है। दो project config settings ज़्यादातर लोगों को उलझा देती हैं: package.json में "type": "module" और tsconfig.json में moduleResolution: "Node16" चाहिए। ये सही हों तो बाकी सब सीधा है।
चरण 1: project सेट करें
mkdir my-mcp-server && cd my-mcp-server
npm init -y
npm install @modelcontextprotocol/sdk zod
npm install -D typescript @types/nodepackage.json में "type": "module" और एक build script जोड़ने के लिए उसे edit करें:
{
"name": "my-mcp-server",
"version": "1.0.0",
"type": "module",
"scripts": {
"build": "tsc",
"start": "node dist/index.js"
},
"dependencies": {
"@modelcontextprotocol/sdk": "^1.11.0",
"zod": "^3.24.0"
},
"devDependencies": {
"typescript": "^5.7.0",
"@types/node": "^22.0.0"
}
}tsconfig.json बनाएं। module और moduleResolution दोनों सेटिंग्स Node16 होनी चाहिए — SDK अंदरूनी तौर पर .js extension वाले imports इस्तेमाल करता है, और पुरानी सेटिंग्स compile errors देती हैं:
{
"compilerOptions": {
"target": "ES2022",
"module": "Node16",
"moduleResolution": "Node16",
"outDir": "./dist",
"rootDir": "./src",
"strict": true,
"esModuleInterop": true,
"skipLibCheck": true
},
"include": ["src/**/*"]
}चरण 2: server लिखें
src/index.ts बनाएं। वही fetch_word_count example जो Python में था, अब TypeScript में। Input schema Zod में लिखी गई है; SDK tools की सूची host को भेजने से पहले उसे JSON Schema में बदल देता है।
// src/index.ts
import { McpServer } from "@modelcontextprotocol/sdk/server/mcp.js";
import { StdioServerTransport } from "@modelcontextprotocol/sdk/server/stdio.js";
import { z } from "zod";
const server = new McpServer({
name: "text-utils",
version: "1.0.0",
});
server.registerTool(
"fetch_word_count",
{
description:
"Count the number of words in a block of text. " +
"Use this when the user asks how many words are in a passage.",
inputSchema: {
text: z.string().describe("The text whose words should be counted."),
},
},
async ({ text }) => ({
content: [
{
type: "text",
text: String(text.trim().split(/\s+/).filter(Boolean).length),
},
],
})
);
async function main() {
const transport = new StdioServerTransport();
await server.connect(transport);
// Use console.error (stderr) — NEVER console.log (stdout corrupts the transport)
console.error("text-utils MCP server started on stdio");
}
main().catch((err) => {
console.error(err);
process.exit(1);
});चरण 3: build और टेस्ट करें
npm run build
# Test with the Inspector
npx @modelcontextprotocol/inspector node dist/index.jsचरण 4: Claude Desktop में जोड़ें
{
"mcpServers": {
"text-utils": {
"command": "node",
"args": ["/absolute/path/to/my-mcp-server/dist/index.js"]
}
}
}Claude Desktop फिर से शुरू करें। Tools मिलने पर hammer icon दिखता है। आप "command" के साथ एक "env" field भी जोड़ सकते हैं, ताकि API keys या दूसरे secrets को hardcode किए बिना अपने server process को दिए जा सकें।
आम गलतियां और उनसे कैसे बचें
ज़्यादातर MCP server की विफलताएं पांच श्रेणियों में आती हैं। इन्हें पहले से जानने से घंटों की debugging बच जाती है:
| गलती | लक्षण | समाधान |
|---|---|---|
| TypeScript में stray stdout | Server कनेक्ट होता है लेकिन tools कभी नतीजा नहीं लौटाते; Inspector में parse errors दिखते हैं | हर console.log() को console.error() से बदलें |
| Host config में relative path | Restart के बाद Claude Desktop में कोई tool नहीं दिखता | Config JSON में अपने project directory का absolute path इस्तेमाल करें |
| "type": "module" गायब (TS) | TypeScript compile Cannot find module errors के साथ विफल होता है | package.json में "type": "module" जोड़ें और tsconfig में module/moduleResolution को Node16 सेट करें |
| अस्पष्ट tool description | Model गलत tool call करता है या arguments गढ़ लेता है | Description को mini-prompt की तरह दोबारा लिखें: वह क्या करता है, inputs का मतलब क्या है, और उसे कब call करना है |
| Config बदलने के बाद server restart नहीं हुआ | Claude Desktop में पुराने tools अब भी दिखते हैं / नए tools गायब हैं | हर config बदलाव के बाद Claude Desktop को पूरी तरह बंद करके फिर से खोलें |
Debugging चेकलिस्ट
- पहले
npx @modelcontextprotocol/inspector <your server command>चलाएं — अगर Inspector कनेक्ट नहीं हो पाता, तो गड़बड़ी आपके server में है, host config में नहीं - अगर Inspector कनेक्ट होता है पर tools नहीं दिखते, तो जांचें कि
mcp.run()/server.connect()कॉल करने से पहले आप tools register कर रहे हैं - अगर tools दिखते हैं पर calls विफल होते हैं, तो schema validation errors के लिए Inspector में raw JSON-RPC tab देखें
- अगर Inspector में सब काम करता है पर Claude Desktop में नहीं, तो अपनी host config का command ठीक उसी से मिलाएं जिससे Inspector ने server लॉन्च किया था
- Stderr output देखें — FastMCP और McpServer दोनों वहीं startup errors लॉग करते हैं
और गहराई से
असली I/O वाला दूसरा tool जोड़ना
एक बार आपका पहला tool काम करने लगे, तो और tools जोड़ना उसी पैटर्न का पालन करता है। यहां एक Python example है जो किसी URL का HTTP status code लाता है — थोड़ा ज़्यादा वास्तविक tool, जो network call करता है:
import httpx
@mcp.tool()
def check_url_status(url: str) -> int:
"""Return the HTTP status code for a given URL.
Use this to verify that a URL is reachable before citing it.
Returns 0 if the request times out or the host is unreachable.
Args:
url: The full URL to check, including https://
"""
try:
response = httpx.get(url, timeout=5, follow_redirects=True)
return response.status_code
except Exception:
return 0एक resource expose करना
Resources read-only data sources हैं जिन्हें host context के रूप में लोड करता है — इन्हें attached files की तरह समझें। Model इन्हें सीधे call नहीं करता; host तय करता है कि कब इन्हें शामिल करना है। Python में @mcp.resource() से एक register करें:
@mcp.resource("config://app/schema")
def get_db_schema() -> str:
"""Current database schema (read-only, for context)."""
return open("schema.sql").read()Secrets के लिए environment variables इस्तेमाल करना
आपका server एक subprocess के रूप में चलता है, इसलिए वह उन environment variables को विरासत में लेता है जो आप host config में पास करते हैं। API keys को कभी source में hardcode न करें। उन्हें host JSON config के env field से पास करें:
{
"mcpServers": {
"my-api-server": {
"command": "uv",
"args": ["--directory", "/path/to/project", "run", "server.py"],
"env": {
"MY_API_KEY": "sk-..."
}
}
}
}Key को os.environ["MY_API_KEY"] (Python) या process.env.MY_API_KEY (TypeScript) से पढ़ें। Host लॉन्च से पहले subprocess के environment में env vars inject करता है।
Streamable HTTP पर कब जाएं
stdio transport लोकल, single-user servers के लिए आदर्श है। Streamable HTTP transport पर तब जाएं जब कई users को एक server साझा करना हो, server को cloud में चलना हो, या stateful connections चाहिए हों। 2024-11-05 spec का HTTP+SSE transport मार्च 2025 में deprecated हो गया था; सभी नए remote servers के लिए Streamable HTTP इस्तेमाल करें (यह 2025-03-26 spec से उपलब्ध है)। आपका tool handler code transport से स्वतंत्र एक जैसा रहता है — केवल mcp.run() call बदलती है।
Production servers के लिए सुरक्षा नियम
- सभी inputs validate करें — JSON Schema की जांच से आगे भी। Path traversal, SQL injection और दूसरे business-logic हमले schema validation को आसानी से पार कर जाते हैं
- Credentials को कड़ाई से सीमित करें — server के service account को केवल वही permissions दें जिनकी उसे सचमुच ज़रूरत है
- Structured errors लौटाएं —
isError: trueके साथ, unhandled exceptions throw करने के बजाय; unhandled exceptions stack traces और internal paths लीक कर सकते हैं - किसी भी Streamable HTTP server पर bearer-token auth जोड़ें — खुला HTTP endpoint बिना auth वाला remote execution जोखिम है
- Destructive tools का ऑडिट करें — जो tools लिखते, मिटाते या भेजते हैं, उनके description में साफ लेबल होना चाहिए ताकि model उन्हें call करने से पहले user से पुष्टि कर ले
अक्सर पूछे जाने वाले प्रश्न
MCP server बनाने के लिए कौन-सी भाषा बेहतर है, Python या TypeScript?
दोनों को आधिकारिक SDKs पूरी तरह सपोर्ट करते हैं और host के नज़रिए से दोनों एक जैसे काम करते हैं। FastMCP के साथ Python सबसे तेज़ prototype बनाने देता है — एक decorator और एक docstring ही काफी हैं। अगर आपकी टीम पहले से Node.js में काम करती है, या आप अपने tool schemas पर compile-time के सख्त types चाहते हैं, तो TypeScript स्वाभाविक विकल्प है। चुनाव अपनी टीम के मौजूदा stack के हिसाब से करें।
क्या मुझे JSON Schema हाथ से लिखना होगा?
नहीं। FastMCP (Python) आपके function के type hints और docstring से अपने-आप JSON Schema बना देता है। TypeScript SDK Zod schemas स्वीकार करता है और उन्हें बदल देता है। Raw JSON Schema केवल तब लिखना पड़ता है जब आप low-level SDK primitives इस्तेमाल करते हैं या ऐसी schema features चाहिए जो high-level wrappers उजागर नहीं करते।
मेरा server MCP Inspector में चलता है, पर Claude Desktop में नहीं। मुझे क्या जांचना चाहिए?
पहले, host config में path absolute है और सही project directory की ओर इशारा करता है, यह पक्का करें। दूसरा, config में command और args ठीक वही हों जो आपने Inspector में चलाए थे। तीसरा, हर config बदलाव के बाद Claude Desktop को पूरी तरह बंद करके फिर से खोलें — बदलाव लागू होने के लिए restart ज़रूरी है। अंत में, startup errors के लिए stderr output देखें; FastMCP और McpServer दोनों वहीं लॉग करते हैं।
क्या एक server में कई tools चला सकता हूं?
हां — आप एक ही server instance में जितने चाहें उतने tools, resources और prompts register कर सकते हैं। इसकी कोई सीमा नहीं है। व्यवहार में, server को एक domain पर केंद्रित रखें (जैसे आपके CRM के लिए एक server, file system के लिए दूसरा), ताकि tool की सूची छोटी रहे और model सही चुन सके। लंबी, बिखरी हुई tools की सूची से model के गलत tool चुनने की संभावना बढ़ती है।
अगर मेरा tool exception फेंके तो क्या होगा?
Unhandled exception के कारण server host को एक error लौटाता है, जिसे model tool की विफलता के रूप में देखता है। बेहतर तरीका है कि आप handler के अंदर exceptions पकड़ें और isError: true के साथ सादी भाषा में व्याख्या वाला जवाब लौटाएं। इससे model उपयोगकर्ता को बता सकता है कि क्या गलत हुआ, और agentic loop में तय कर सकता है कि कैसे उबरना है — session को क्रैश करने के बजाय।
क्या मैं अपने MCP server को Claude Desktop के अलावा दूसरे AI clients से जोड़ सकता हूं?
हां। MCP एक open standard है, जिसे Claude Desktop, Claude Code, Cursor, Windsurf, VS Code Copilot और बढ़ती संख्या में दूसरे hosts सपोर्ट करते हैं। आज आप जो server लिखते हैं, वह बिना बदलाव के इनमें से किसी से भी जुड़ जाता है — बस हर host की config file में उसे जोड़ना होता है। एक बार लिखें, कहीं भी जोड़ें — यही इस standard पर बनाने का मुख्य फायदा है।
