मेरा MCP सर्वर बस काम करना बंद कर देता था। कोई क्रैश डंप नहीं। लॉग्स में कोई स्टैक ट्रेस नहीं। क्लाइंट्स बिना किसी शिकायत के जुड़े रहते थे, फिर कुछ घंटों के बाद सब कुछ शांत हो जाता था। रिक्वेस्ट गायब हो जाती थीं और दूसरी तरफ मौजूद AI एजेंट को खाली हवा के अलावा कुछ नहीं मिलता था।

Model Context Protocol (MCP) इकोसिस्टम में यह एक निराशाजनक रूप से आम कहानी है। यह प्रोटोकॉल परिभाषित करता है कि AI एजेंट बाहरी टूल्स को कैसे खोजते हैं और उन्हें कैसे कॉल करते हैं, लेकिन इसका स्पेसिफिकेशन यह मानकर चलता है कि आप एरर्स (errors) को खुद संभाल लेंगे। अधिकांश ट्यूटोरियल और शुरुआती इम्प्लीमेंटेशन इस हिस्से को छोड़ देते हैं। वे केवल 'हैप्पी पाथ' (happy path) पर ध्यान केंद्रित करते हैं: एक फंक्शन को एनोटेट करें, उसे सर्वर के माध्यम से एक्सपोज़ करें, और एक साफ परिणाम (result) लौटाएं। वे शायद ही कभी यह दिखाते हैं कि क्या होता है जब आपके बाहरी API में नेटवर्क की समस्या आती है, या जब मॉडल किसी पैरामीटर का नाम गलत बताता है (hallucinates) और कचरा इनपुट भेजता है। इसका परिणाम एक ऐसा नाजुक सर्वर होता है जो देखने में तो स्वस्थ लगता है लेकिन वास्तव में घंटों से बंद पड़ा होता है।

खाली रिस्पॉन्स (Blank Responses) क्रैश से भी बदतर क्यों हैं

जब MCP टूल हैंडलर में कोई अनहैंडल्ड एक्सेप्शन (unhandled exception) आ जाता है, तो ट्रांसपोर्ट लेयर अक्सर उसे निगल लेती है। सर्वर प्रोसेस जीवित रहती है, सॉकेट खुला रहता है, लेकिन क्लाइंट को एक खाली रिस्पॉन्स मिलता है। यह एक स्पष्ट क्रैश की तुलना में अधिक खतरनाक है क्योंकि आपका मॉनिटरिंग सिस्टम इसे नोटिस नहीं कर पाएगा। प्रोसेस अभी भी चल रही है। पोर्ट अभी भी सुन रहा है। फिर भी हर टूल कॉल कुछ भी नहीं लौटाती।

AI मॉडल चुप्पी को विफलता (failure) के रूप में नहीं देखता। वह चुप्पी को एक सफल कॉल के रूप में देखता है जिसने कोई डेटा उत्पन्न नहीं किया। वह खाली रिस्पॉन्स मॉडल को खुद से चीजें बनाने (improvise) के लिए प्रशिक्षित करता है। यह खालीपन को भरने के लिए तथ्यों की कल्पना (hallucinate) करने लगता है, या फिर वही खराब कॉल बार-बार दोहराने के लूप में फंस जाता है। नेटवर्क टाइमआउट या किसी गलत टूल आर्गुमेंट जैसी छोटी समस्याओं को इस तरह का व्यवहार करने की अनुमति कभी नहीं दी जानी चाहिए।

रैपर पैटर्न (The Wrapper Pattern): बचाव की तीन लाइनें

मैंने हर टूल हैंडलर को एक पतली एरर-रिकवरी लेयर (error-recovery layer) में लपेटकर (wrap करके) इसे ठीक किया। रैपर हर संभावित विफलता का अनुमान लगाने की कोशिश नहीं करता है। यह उन्हें वर्गीकृत करता है और उसी के अनुसार जवाब देता है।

ConnectionError और TimeoutError
जब आपका सर्वर किसी बाहरी API से बात करता है और नेटवर्क में अस्थिरता आती है, तब ये एरर्स आते हैं। स्वाभाविक समाधान पूरे MCP सर्वर प्रोसेस को रीस्टार्ट करना लगता है। ऐसा न करें। रीबूट करने से सक्रिय क्लाइंट कनेक्शन टूट जाते हैं, इन-मेमोरी स्टेट (in-memory state) साफ हो जाती है, और पूरी री-इनिशियलाइजेशन (re-initialization) करनी पड़ती है। इसके बजाय, कनेक्शन विफलता को पकड़ें (catch करें) और केवल उस ट्रांसपोर्ट लेयर या HTTP क्लाइंट को फिर से कनेक्ट करें जिसका आपका टूल उपयोग करता है। सर्वर तुरंत अगले अनुरोध के लिए तैयार रहता है।

ValueError
यह तब होता है जब AI क्लाइंट गलत तरीके से बनाए गए (malformed) आर्गुमेंट्स भेजता है। हो सकता है कि मॉडल ने कोई नया पैरामीटर बना लिया हो, जहाँ पूर्णांक (integer) की आवश्यकता थी वहाँ स्ट्रिंग (string) भेज दी हो, या कोई आवश्यक फ़ील्ड भूल गया हो। यदि आप इसे अनहैंडल्ड रहने देते हैं, तो क्लाइंट को या तो क्रैश मिलता है या खाली जवाब। इसे रैपर के अंदर ही पकड़ें, फिर एक स्पष्ट और विशिष्ट संदेश तैयार करें जो मॉडल को ठीक-ठीक बताए कि क्या गलत हुआ। समझाएं कि कौन सा पैरामीटर विफल रहा और क्या अपेक्षित था। अधिकांश आधुनिक AI मॉडल उस संदेश को पढ़ेंगे और अगले ही टर्न में खुद को सुधार लेंगे। एक अस्पष्ट एरर रीजनिंग साइकिल (reasoning cycle) को बर्बाद कर देती है। एक सटीक एरर समस्या को तुरंत ठीक कर देती है।

General Exceptions
एक सुरक्षा जाल (safety net) रखें। यदि कोई एरर ऊपर दी गई श्रेणियों से बाहर है, तो अपने लिए विवरण लॉग करें और क्लाइंट को एक साफ, सामान्य विफलता रिस्पॉन्स लौटाएं। यह किसी एक अजीब एज केस (edge case) को सभी के लिए सेशन खत्म करने से रोकता है। सर्वर सुरक्षित रहता है, क्लाइंट को संकेत मिलता है कि कुछ विफल हुआ है, और आपके पास बाद में डिबग करने के लिए लॉग्स में पर्याप्त संदर्भ (context) रहता है।

isError फ्लैग पर समझौता नहीं किया जा सकता

यहाँ वह विवरण है जो वास्तव में तय करता है कि आपका समाधान काम करेगा या नहीं। MCP रिस्पॉन्स में एक isError boolean फ़ील्ड शामिल होता है। यदि कोई एक्सेप्शन होता है और आप isError को true सेट किए बिना एरर मैसेज लौटाते हैं, तो क्लाइंट उस एरर टेक्स्ट को एक सफल टूल परिणाम (result) के रूप में मानता है।

कल्पना कीजिए कि आपके बाहरी API की रेट लिमिट (rate limit) समाप्त हो गई है। आप एक्सेप्शन को पकड़ते हैं और स्ट्रिंग "API rate limit exceeded" लौटाते हैं, लेकिन isError को false ही रहने देते हैं। क्लाइंट उस स्ट्रिंग को मॉडल के कॉन्टेक्स्ट विंडो (context window) में इस तरह पास कर देता है जैसे कि वह टूल का वास्तविक आउटपुट हो। मॉडल फिर उस टेक्स्ट पर डेटा की तरह तर्क करने की कोशिश करता है। वह सारांश (summary) में उस एरर को उद्धृत कर सकता है, या इससे भी बुरा, वह उस एरर टेक्स्ट और अन्य तथ्यों के बीच संबंध की कल्पना (hallucinate) कर सकता है। आपने बुनियादी ढांचे की एक अस्थायी समस्या को गलत सूचना के स्रोत में बदल दिया है।

Always set isError to true when you are returning an error payload. This gives the client a clear signal that the tool call failed, which lets the model decide whether to retry, ask for clarification, or try a different tool entirely.

Know What to Catch and What to Kill

Do not wrap your entire server in a blind try-catch that swallows everything. Some errors mean the server should stop immediately. If a required environment variable is missing on startup, or your configuration file is corrupt, no amount of request-level catching will help. Create a specific exception class for fatal errors like these and let them crash the process.

The rule is simple. If the error is temporary or isolated to a single request, catch it and recover. If the error means every subsequent request is guaranteed to fail, let the server die loudly. A fast failure on startup is infinitely better than a server that limps along for days in a broken state.

Add Observability Before You Need It

Once you have the wrapper in place, pair it with structured logging. Log every tool call and its outcome in JSON format. Include the tool name, the raw arguments, the latency, and whether it succeeded, failed, or retried.

This discipline pays off quickly. When you notice a spike in errors, you can filter by tool and spot patterns in minutes. Maybe a specific external API starts throwing timeouts at the same time every day, pointing to a scheduled maintenance window you did not know about. Maybe one tool receives consistently malformed arguments, revealing a prompt engineering flaw upstream. Plain text logs buried in stack traces make this detective work painful. Structured JSON makes it trivial.

The Production Result

I have run this wrapper pattern on two production MCP servers for the past three weeks. In that window, I have seen zero silent failures. Before adding the wrapper, I averaged roughly one unexplained failure every day. The pattern is not complex, but its impact is outsized because it separates survivable noise from real problems.

Silent failures cost more than crashes. A crash triggers your alerting system. Silence just erodes trust. One day your AI agent returns useful tool data, and the next day it starts making things up because the server stopped answering hours ago. The wrapper pattern closes that gap. It keeps your server running through minor turbulence, gives the model enough context to fix its own mistakes, and ensures that when something truly fatal goes wrong, you hear about it immediately.

If you are building MCP tools today, start with the wrapper and the isError flag. Everything else is just cleanup.