Back to Blog
BlogApril 6, 2026327

अपना पहला एमसीपी सर्वर कैसे बनाएं: चरण-दर-चरण ट्यूटोरियल

अपना पहला एमसीपी सर्वर कैसे बनाएं: चरण-दर-चरण ट्यूटोरियल

पूर्वापेक्षाएँ

शुरू करने से पहले, सुनिश्चित करें कि आपके पास ये तैयार हैं:

  • Python 3.10 या उच्चतर इंस्टॉल किया हुआ।
  • Python फ़ंक्शंस और async कोड की बुनियादी समझ।
  • uv (तेज़ Python पैकेज मैनेजर) – इंस्टॉल करें curl -LsSf https://astral.sh/uv/install.sh | sh के साथ।
  • एक MCP क्लाइंट जैसे Claude Desktop (claude.ai/download से डाउनलोड करें और इसे अपडेट रखें)।
  • एक टेक्स्ट एडिटर या IDE।

किसी पूर्व MCP अनुभव की आवश्यकता नहीं है। हम एक व्यावहारिक मौसम MCP सर्वर बनाएंगे जो AI को नेशनल वेदर सर्विस एपीआई के माध्यम से यू.एस. मौसम अलर्ट और पूर्वानुमान प्राप्त करने की सुविधा देता है।

चरण 1: पर्यावरण सेट अप करें

एक स्वच्छ प्रोजेक्ट डायरेक्टरी बनाएं और इसे uv के साथ प्रारंभ करें:

mkdir weather-mcp-server
cd weather-mcp-server
uv init
uv venv
source .venv/bin/activate  # Windows पर: .venv\Scripts\activate
uv add "mcp[cli]" httpx

यह आधिकारिक MCP Python SDK (FastMCP) और एपीआई कॉल्स के लिए httpx इंस्टॉल करता है। अब आपके प्रोजेक्ट में एक pyproject.toml और वर्चुअल एनवायरनमेंट है।

अपेक्षित आउटपुट: एक नया .venv फ़ोल्डर और uv.lock में निर्भरताएँ सूचीबद्ध।

चरण 2: MCP सर्वर कोड बनाएं

मुख्य फ़ाइल बनाएं:

touch weather.py

इस पूर्ण, चलाने के लिए तैयार कोड को weather.py में पेस्ट करें:

from typing import Any
import httpx
from mcp.server.fastmcp import FastMCP

# MCP सर्वर प्रारंभ करें
mcp = FastMCP("weather")

NWS_API_BASE = "https://api.weather.gov"
USER_AGENT = "weather-app/1.0"

# NWS एपीआई को सुरक्षित रूप से कॉल करने के लिए सहायक फ़ंक्शन
async def make_nws_request(url: str) -> dict[str, Any] | None:
    headers = {"User-Agent": USER_AGENT, "Accept": "application/geo+json"}
    async with httpx.AsyncClient() as client:
        try:
            response = await client.get(url, headers=headers, timeout=30.0)
            response.raise_for_status()
            return response.json()
        except Exception:
            return None

# AI के लिए मौसम अलर्ट को सुंदर ढंग से फ़ॉर्मेट करें
def format_alert(feature: dict) -> str:
    props = feature["properties"]
    return f"""
Event: {props.get("event", "Unknown")}
Area: {props.get("areaDesc", "Unknown")}
Severity: {props.get("severity", "Unknown")}
Description: {props.get("description", "No description available")}
Instructions: {props.get("instruction", "No specific instructions provided")}
"""

# टूल 1: यू.एस. राज्य के लिए सक्रिय मौसम अलर्ट प्राप्त करें
@mcp.tool()
async def get_alerts(state: str) -> str:
    """यू.एस. राज्य के लिए वर्तमान मौसम अलर्ट प्राप्त करें (जैसे, "CA" या "TX")."""
    url = f"{NWS_API_BASE}/alerts/active/area/{state.upper()}"
    data = await make_nws_request(url)
    if not data or "features" not in data:
        return "Unable to fetch alerts or no alerts found."
    if not data["features"]:
        return f"No active alerts for {state.upper()}."
    alerts = [format_alert(f) for f in data["features"]]
    return "\n---\n".join(alerts)

टूल 2: किसी भी अक्षांश/देशांतर के लिए 5–दिन का पूर्वानुमान प्राप्त करें

@mcp.tool() async def get_forecast(latitude: float, longitude: float) -> str: """किसी विशिष्ट अक्षांश और देशांतर के लिए मौसम का पूर्वानुमान प्राप्त करें।""" points_url = f"{NWS_API_BASE}/points/{latitude},{longitude}" points_data = await make_nws_request(points_url) if not points_data: return "इस स्थान के लिए पूर्वानुमान डेटा प्राप्त करने में असमर्थ।" forecast_url = points_data["properties"]["forecast"] forecast_data = await make_nws_request(forecast_url) if not forecast_data: return "विस्तृत पूर्वानुमान प्राप्त करने में असमर्थ।" periods = forecast_data["properties"]["periods"][:5] forecasts = [] for period in periods: forecast = f""" {period["name"]}: तापमान: {period["temperature"]}°{period["temperatureUnit"]} हवा: {period["windSpeed"]} {period["windDirection"]} पूर्वानुमान: {period["detailedForecast"]} """ forecasts.append(forecast) return "\n---\n".join(forecasts)

def main(): mcp.run(transport="stdio")

if name == "main": main()


**समझाए गए मुख्य अवधारणाएँ:**
- `@mcp.tool()` डेकोरेटर सामान्य async फ़ंक्शंस को MCP टूल्स में बदल देते हैं जिन्हें AI खोज और कॉल कर सकता है।
- सर्वर **stdio** (स्टैण्डर्ड इनपुट/आउटपुट) पर चलता है – यह स्थानीय MCP सर्वर के लिए डिफ़ॉल्ट है।
- सभी लॉगिंग stderr पर जानी चाहिए (FastMCP यह स्वचालित रूप से संभालता है)।

## चरण 3: सर्वर को स्थानीय रूप से परीक्षण करें

सर्वर चलाएँ:

```bash
uv run weather.py

अपेक्षित आउटपुट: टर्मिनल खुला और शांत रहता है (यह stdio सर्वर के लिए सामान्य है)। आपको JSON-RPC संदेश केवल तभी दिखेंगे जब कोई MCP क्लाइंट कनेक्ट करता है।

अगले चरण के लिए इस टर्मिनल को चलते हुए छोड़ दें।

चरण 4: MCP सर्वर को Claude Desktop से कनेक्ट करें

  1. Claude Desktop खोलें।
  2. कॉन्फ़िग फ़ाइल को यहाँ बनाएँ या संपादित करें:
    • macOS: ~/Library/Application Support/Claude/claude_desktop_config.json
    • Windows: %APPDATA%\Claude\claude_desktop_config.json

इस सटीक एंट्री को जोड़ें (अपने वास्तविक फ़ोल्डर पथ से /ABSOLUTE/PATH/TO/weather-mcp-server को बदलें):

{
  "mcpServers": {
    "weather": {
      "command": "uv",
      "args": [
        "--directory",
        "/ABSOLUTE/PATH/TO/weather-mcp-server",
        "run",
        "weather.py"
      ]
    }
  }
}
  1. Claude Desktop को पूरी तरह बंद करें (macOS पर Cmd+Q या Windows पर सिस्टम ट्रे से बंद करें) और इसे पुनः आरंभ करें।
  2. Claude में, “Add files, connectors, and more” पर क्लिक करें → Connectors पर होवर करें → आपको “weather” सूचीबद्ध दिखना चाहिए।

चरण 5: AI के साथ अपने MCP सर्वर का उपयोग करें

Claude Desktop में इन प्रॉम्प्ट्स को आज़माएँ:

  • “Texas में सक्रिय मौसमी चेतावनियाँ क्या हैं?”
  • “San Francisco के लिए मुझे पूर्वानुमान दें (lat 37.77, long -122.41 का उपयोग करें)।”
  • “California में चेतावनियाँ जाँचें और मुझे बताएँ कि क्या मुझे किसी चीज़ के लिए तैयारी करने की आवश्यकता है।”

Claude स्वचालित रूप से टूल्स को खोज लेगा, पहली बार आपकी स्वीकृति माँगेगा, और फॉर्मेटेड परिणाम लौटाएगा।

अपेक्षित व्यवहार: AI आपके टूल्स को पर्दे के पीछे कॉल करेगा और प्राकृतिक–भाषा उत्तर दिखाएगा।

सामान्य समस्याएं और समस्या निवारण

  • सर्वर Claude में दिखाई नहीं देता:

    • JSON मान्य है या नहीं दोबारा जांचें (कोई ट्रेलिंग कॉमा नहीं)।
    • कॉन्फ़िग में पूर्ण पथ का उपयोग करें।
    • Claude को पूरी तरह से पुनरारंभ करें।
    • लॉग्स चेक करें: ~/Library/Logs/Claude/mcp*.log (macOS) या Windows पर इसके समतुल्य।
  • API त्रुटियां या डेटा नहीं:

    • NWS API केवल U.S. स्थानों के लिए काम करता है।
    • दो-अक्षर वाले राज्य कोड (CA, TX, आदि) का उपयोग करें।
    • निर्देशांक मान्य अक्षांश/देशांतर होने चाहिए।
  • “Command not found”:

    • सुनिश्चित करें कि uv आपके PATH में है और वर्चुअल एनवायरनमेंट सक्रिय है।
    • स्थापना सत्यापित करने के लिए uv --version चलाएं।
  • टाइमआउट या धीमी प्रतिक्रिया:

    • आवश्यक हो तो make_nws_request में टाइमआउट बढ़ाएं।
    • NWS की दर सीमाएं हैं – प्रोडक्शन में लगातार अनुरोध भेजने से बचें।
  • अनुमति संबंधी समस्याएं:

    • macOS पर, System Settings → Privacy & Security में Claude Desktop को पूर्ण डिस्क पहुंच प्रदान करें।

अगले कदम

  • अधिक टूल्स जोड़ें: समान @mcp.tool() पैटर्न का उपयोग करके डेटाबेस, GitHub, Slack, या अपने स्वयं के API के लिए टूल्स बनाएं।
  • संसाधन और प्रॉम्प्ट्स जोड़ें: फ़ाइल-जैसे डेटा और पुन: प्रयोज्य निर्देशों के लिए mcp.resource() और mcp.prompt() का उपयोग करें।
  • दूरस्थ रूप से डिप्लॉय करें: HTTP/SSE ट्रांसपोर्ट पर स्विच करें और AWS Lambda, Vercel, या किसी भी सर्वर पर होस्ट करें (FastMCP stateless_http=True का समर्थन करता है)।
  • कई भाषाओं का समर्थन करें: समान कार्यक्षमता के लिए आधिकारिक TypeScript, Go, या Rust SDKs आज़माएं। -\ अपना सर्वर साझा करें: रिपो प्रकाशित करें ताकि अन्य लोग इसे npx या Docker के माध्यम से जोड़ सकें।

अब आपके पास एक पूरी तरह कार्यात्मक MCP सर्वर है जिसे कोई भी संगत AI क्लाइंट उपयोग कर सकता है। प्रयोग करें, टूल्स का विस्तार करें, और आज ही शक्तिशाली AI एकीकरण बनाना शुरू करें!

Share this article

Referenced Tools

Browse entries that are adjacent to the topics covered in this article.

Explore directory
PaidSync.ai logo

PaidSync.ai

MCP Servers

PaidSync.ai is a hosted MCP server for paid advertising that exposes 610+ tools across 14 advertising, analytics, tagging, and commerce platforms. It supports read/write workflows from AI clients including ChatGPT, Claude, Gemini, Cursor, Claude Code, and Codex, with a free plan.

Hyper3D MCP logo

Hyper3D MCP

MCP Servers

Hyper3D MCP is Hyper3D's hosted Rodin MCP server for creating 3D assets from natural-language instructions or supported references, checking asynchronous generation progress, and retrieving model download links inside AI coding clients. Official setup is documented for Codex, Claude Code, and Kimi Code CLI.

Semrush MCP Server logo

Semrush MCP Server

MCP Servers

Semrush MCP connects AI assistants to Semrush APIs for keyword, backlink, website traffic and market research.

Browserbase MCP Server logo

Browserbase MCP Server

MCP Servers

Browserbase MCP gives AI agents browser automation tools powered by Stagehand and Browserbase browser sessions.

SerpApi MCP Server logo

SerpApi MCP Server

MCP Servers

SerpApi MCP exposes search engine results to AI assistants through SerpApi, supporting live research and structured search data retrieval.

Similarweb MCP Server logo

Similarweb MCP Server

MCP Servers

Similarweb MCP brings website traffic and digital market intelligence into AI assistants for competitive research.

a2a mcp

Search the directory

Find entries, skills, articles, and categories. Use arrow keys to select and Enter to open.