Back to Course|MCP Mastery: Building AI-Powered Integrations with Model Context Protocol
Lab

Build a Knowledge Base MCP Server

45 min
Intermediate
3 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:

  1. Stores documents in an internal dictionary:

    • Documents have: id, title, content, tags
  2. Lists available resources with list_resources() -> list[dict]:

    • Returns list of resources for each document
    • Each resource has: uri, name, description
  3. Reads a resource with read_resource(uri: str) -> str:

    • Parses document ID from URI (format: doc://id)
    • Returns document content
    • Raises ValueError if not found
  4. Defines tools with get_tool_definitions() -> list[dict]:

    • search_documents: Search by query string
    • add_document: Add new document
  5. 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