Tools, Resources, and Prompts
Combining Capabilities
4 min read
The real power of MCP comes from combining tools, resources, and prompts into cohesive systems.
A Complete Example: Knowledge Base Server
Let's build a knowledge base server that combines all MCP capabilities:
from mcp.server import Server
from mcp.types import Tool, Resource, Prompt, TextContent
server = Server(name="knowledge-base")
# 1. RESOURCES: Expose documents
@server.list_resources()
async def list_resources():
docs = await db.get_all_documents()
return [
Resource(
uri=f"doc://{doc.id}",
name=doc.title,
description=f"Document: {doc.title}",
mimeType="text/markdown"
)
for doc in docs
]
@server.read_resource()
async def read_resource(uri: str):
doc_id = uri.replace("doc://", "")
doc = await db.get_document(doc_id)
return [TextContent(type="text", text=doc.content)]
# 2. TOOLS: Search and modify
@server.list_tools()
async def list_tools():
return [
Tool(
name="search_knowledge",
description="Search the knowledge base",
inputSchema={
"type": "object",
"properties": {
"query": {"type": "string"},
"limit": {"type": "integer", "default": 5}
},
"required": ["query"]
}
),
Tool(
name="add_document",
description="Add a new document to the knowledge base",
inputSchema={
"type": "object",
"properties": {
"title": {"type": "string"},
"content": {"type": "string"},
"tags": {"type": "array", "items": {"type": "string"}}
},
"required": ["title", "content"]
}
)
]
# 3. PROMPTS: Standardized interactions
@server.list_prompts()
async def list_prompts():
return [
Prompt(
name="summarize_topic",
description="Summarize all documents on a topic",
arguments=[
PromptArgument(name="topic", required=True)
]
)
]
Workflow: AI Using All Capabilities
Here's how an AI might use this server:
- Read resource to get full document content
- Call tool to search for related documents
- Use prompt to structure the summary request
User: "Summarize our AI security policies"
AI Actions:
1. search_knowledge(query="AI security policies") → Gets doc IDs
2. read_resource("doc://policy-123") → Gets full content
3. read_resource("doc://policy-456") → Gets more content
4. Summarizes based on documents
Resource + Tool Coordination
Design resources and tools to work together:
# Resource: Read-only view
Resource(uri="user://123", name="User 123 Profile")
# Tool: Modify user
Tool(name="update_user", description="Update user profile")
Best Practices
| Practice | Reason |
|---|---|
| Clear separation | Resources for reading, tools for actions |
| Consistent URIs | Makes resources predictable |
| Related naming | user://123 with update_user tool |
| Document relationships | Explain in descriptions |
Now let's apply these patterns in a hands-on lab. :::