MCP सर्वर रिसोर्सेज़ परिभाषित करना
आप अपने करेंसी कन्वर्ज़न वाले MCP सर्वर का विस्तार कर रहे हैं ताकि उसे उस API द्वारा समर्थित करेंसीज़ की सूची तक पहुँच मिल सके जिसका आप उपयोग करते हैं. European Central Bank currencies.txt नाम की फ़ाइल में करेंसी कोड्स की सूची प्रकाशित करता है, जो आपके सर्वर डायरेक्टरी में उपलब्ध है. क्लाइंट इसे इस लिए उपयोग कर सकता है कि LLM टूल फंक्शंस को सही आर्ग्यूमेंट वैल्यूज़ दे रहा है या नहीं, यह सुनिश्चित किया जा सके.
आपका काम get_currencies() नाम का MCP रिसोर्स परिभाषित करना है जो currencies.txt की सामग्री पढ़े.
यह अभ्यास पाठ्यक्रम का हिस्सा है
Model Context Protocol (MCP) परिचय
अभ्यास निर्देश
- सही डेकोरेटर और
"file://currencies.txt"URI का उपयोग करकेget_currencies()फंक्शन को एक रिसोर्स में बदलें. get_currencies()फंक्शन को पूरा करें ताकि वहcurrencies.txtफ़ाइल को खोले और उसकी सामग्री पढ़े.- रिसोर्स सही तरह काम कर रहा है या नहीं, यह जाँचने के लिए
get_currencies()को कॉल करने का परिणाम प्रिंट करें.
इंटरैक्टिव व्यावहारिक अभ्यास
इस अभ्यास को इस नमूना कोड को पूरा करके आज़माएँ।
from mcp.server.fastmcp import FastMCP
mcp = FastMCP("Currency Converter")
# Define a resource for the currencies file
____
def get_currencies() -> str:
"""
Get the list of currency names published by the European Central Bank for currency conversion.
Returns:
Contents of the currencies.txt file with currency names
"""
# Open currencies.txt and read the data
try:
with open('____', 'r') as f:
content = f.____
return content
except FileNotFoundError:
return "currencies.txt file not found"
# Test the resource function
print(____)