Lab
Build a Knowledge Base MCP Server
45 min
Intermediate3 Free Attempts
Instructions
Objective
Build a Python class that simulates an MCP knowledge base server with search, document retrieval, and document addition capabilities.
Requirements
Create a class KnowledgeBase that:
-
Stores documents in an internal dictionary:
- Documents have:
id,title,content,tags
- Documents have:
-
Lists available resources with
list_resources() -> list[dict]:- Returns list of resources for each document
- Each resource has:
uri,name,description
-
Reads a resource with
read_resource(uri: str) -> str:- Parses document ID from URI (format:
doc://id) - Returns document content
- Raises
ValueErrorif not found
- Parses document ID from URI (format:
-
Defines tools with
get_tool_definitions() -> list[dict]:search_documents: Search by query stringadd_document: Add new document
-
Executes tools with
call_tool(name: str, arguments: dict) -> dict:search_documents: Returns matching documents (title contains query)add_document: Adds document, returns new ID
Example Usage
kb = KnowledgeBase()
# Add documents
result = kb.call_tool("add_document", {
"title": "Python Basics",
"content": "Python is a programming language...",
"tags": ["python", "programming"]
})
# {"success": True, "id": "doc_1"}
# Search documents
result = kb.call_tool("search_documents", {"query": "Python"})
# {"results": [{"id": "doc_1", "title": "Python Basics", ...}]}
# List resources
resources = kb.list_resources()
# [{"uri": "doc://doc_1", "name": "Python Basics", ...}]
# Read resource
content = kb.read_resource("doc://doc_1")
# "Python is a programming language..."
Hints
- Use a counter for generating unique IDs (doc_1, doc_2, etc.)
- Search should be case-insensitive
- Resources should be dynamically generated from stored documents
Grading Rubric
Correctly implements document storage and list_resources25 points
read_resource correctly parses URI and returns content25 points
get_tool_definitions returns valid MCP tool schemas25 points
call_tool implements search and add with correct behavior25 points
Your Solution
This lab requires Python
🐍Python(required)
3 free attempts remaining