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—no manual schema writing needed
- Significantly reduces boilerplate compared to manual tool definition
Tool Definition Pattern
- Use @MCP. tool decorator to define a tool
- Provide tool name and description
- Define function with typed parameters using Pydantic Field
- SDK automatically converts function signature into proper JSON schema
Tool 1: Read Document
- Name: read_doc_contents
- Purpose: Retrieve contents of a document by ID
- 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
Tool 2: Edit Document
- Name: edit_document
- Purpose: Modify document content using find-and-replace
- 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 string . replace() method on document contents
- Error handling: Raise ValueError if document ID doesn't exist
Key Imports
- Import Field from pydantic to define tool parameters with descriptions
Takeaways
- MCP Python SDK simplifies server creation to one line of code and automatically generates JSON schemas for tools, eliminating manual boilerplate
- Tools are defined using the @MCP. tool decorator with typed Pydantic Field parameters that the SDK converts to proper JSON schemas
- Two core tools needed: read_doc_contents (retrieves document by ID) and edit_document (modifies content via find-and-replace)
- Both tools require error handling to raise ValueError when a document ID doesn't exist
- Import Field from pydantic to add descriptions to tool parameters
Flashcards 10 cards
Question
click to reveal · ←/→
Answer
click to flip back
Knowledge check 1 questions