Summary audio
No audio recap for this lesson.
Study notes
Overview
- Building an MCP (Model Context Protocol) server to add functionality to an existing CLI chatbot
- Server will provide tools that Claude can use to interact with documents
- Implementation goes in mcpserver. py file in the project root
MCP Python SDK Benefits
- Official MCP Python SDK simplifies server and tool creation
- Creates MCP server with just one line of code
- Automatically generates JSON schemas for tools behind the scenes
- Much easier than manually writing JSON schema definitions
Tool Definition Pattern
- Use @MCP. tool decorator to define a tool
- Specify tool name and description
- Define function with typed parameters using Pydantic Field class
- SDK automatically generates the required JSON schema
First Tool: Read Document
- Name: read_doc_contents
- Purpose: Read and return contents of a document
- Parameter: doc_id (string) - ID of document to read
- Implementation: Look up document in docs dictionary and return its contents
- Error handling: Raise ValueError if document ID doesn't exist
Second Tool: Edit Document
- Name: edit_document
- Purpose: Find and replace text within a document
- Parameters:
- doc_id (string) - ID of document to edit
- old_string (string) - Text to find (must match exactly including whitespace)
- new_string (string) - Replacement text
- Implementation: Use Python string . replace() method
- Error handling: Raise ValueError if document ID doesn't exist
Key Imports
- from pydantic import Field - For defining tool parameters with descriptions
Takeaways
- MCP Python SDK simplifies server creation and automatically generates JSON schemas for tools, eliminating manual schema writing
- Use @MCP. tool decorator with typed Pydantic Field parameters to define tools that Claude can call
- read_doc_contents tool retrieves document text by doc_id from a docs dictionary, raising ValueError if ID doesn't exist
- edit_document tool finds and replaces exact text matches within documents using Python's . replace() method
- Both tools require error handling for invalid document IDs
Flashcards 8 cards
Question
click to reveal · ←/→
Answer
click to flip back
Knowledge check 1 questions