Defining MCP Server Resources
You're expanding your MCP server for currency conversion to give it access to the list of currencies supported by the API you're using. The European Central Bank publishes a list of currency codes in a file called currencies.txt that is available in your server directory. This could be used by the client to ensure that the LLM is passing the correct argument values to the tool functions.
Your task is to define an MCP resource called get_currencies() that reads the contents of currencies.txt.
Este ejercicio forma parte del curso
Introduction to Model Context Protocol (MCP)
Instrucciones del ejercicio
- Use the correct decorator and the
"file://currencies.txt"URI to convert theget_currencies()function into a resource. - Complete the
get_currencies()function to open and read the contents of thecurrencies.txtfile. - Print the result of calling
get_currencies()to verify the resource works correctly.
Ejercicio interactivo práctico
Prueba este ejercicio y completa el código de muestra.
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(____)