MCP Learning Series - Part 5
We know that MCP is a Client-Server Architecture. Both the sides offer services to each other. From the server side it offers 3 different services (Primitive) to the client.
1) Tools - Used by the LLM
2) Resources - Used by the AI Applications
3) Prompts - Used by the users interacting with the AI Applications
Tools @mcp.tool() - These are the functions used by the LLM's to perform an action.
Resources @mcp.resource() - Refers to any data sources which is access by the AI application including reading file, accessing the database via SQL, reading a CSV file or connecting to a DataLake.
Prompts @mcp.prompt() - Structured template which gets inputs from the user and perform an action.
How to add a prompt and resources to existing MCP Server:
@mcp.prompt()
def get_prompt(topic: str) -> str:
"""
Returns a prompt for the given topic which will do a detailed analysis on the topic.
Args:
topic (str): The topic to analyze.
"""
return (
f"Do a comprehensive, detailed analysis on the topic of {topic}. and provide "
f"a summary of the key points. "
"The analysis should be thorough and cover all relevant aspects of the topic. "
"The summary should be concise and highlight the most important findings. "
)
This prompt takes an input on a topic and uses AI application (LLM) to return the data with detailed analysis.
Let's see how this prompt works with Claude:
Let's add resource: I am using a Dictionary with dummy library data as resource.
LIBRARY_DATA = {
"books": [
{
"id": "B001",
"title": "Artificial Intelligence: A Modern Approach",
"authors": ["Stuart Russell", "Peter Norvig"],
"isbn": "978-0134610993",
"category": "Computer Science",
"publisher": "Pearson",
"publication_year": 2020,
"copies_total": 5,
"copies_available": 2,
"location": "CS-Section-A-Shelf-12",
"status": "available",
"last_updated": "2024-01-15T10:30:00Z",
},
{
"id": "B002",
"title": "Clean Code: A Handbook of Agile Software Craftsmanship",
"authors": ["Robert C. Martin"],
"isbn": "978-0132350884",
"category": "Software Engineering",
"publisher": "Prentice Hall",
"publication_year": 2008,
"copies_total": 3,
"copies_available": 0,
"location": "SE-Section-B-Shelf-05",
"status": "checked_out",
"last_updated": "2024-01-20T14:15:00Z",
}
]
}
@mcp.resource("library://catalog")
def get_full_catalog() -> str:
"""
Returns the complete library catalog with all books and their detailed information.
"""
try:
books = LIBRARY_DATA["books"]
catalog = {
"total_books": len(books),
"last_updated": datetime.now().isoformat(),
"books": books,
}
return json.dumps(catalog, indent=2)
except Exception as e:
return json.dumps({"error": str(e)})Testing with Claude:





Comments
Post a Comment