Claude With The Anthropic Api
← All lessons
Lesson 65Claude With The Anthropic Api

Implementing a client

Summary audio

No audio recap for this lesson.

Study notes

Overview

  • The MCP client wraps a client session, which is the actual connection to the MCP server
  • The client session is part of the MCP Python SDK
  • The client class exists primarily to manage resource cleanup (connection lifecycle)
  • Cleanup code is handled in the connect function and async enter/async exit functions

Purpose of the Client

  • Exposes server functionality to the rest of the codebase
  • Allows other code to access tools and prompts defined by the MCP server
  • Acts as an intermediary between the application and the MCP server

Key Functions to Implement

list_tools()

  • Calls self. session. list_tools() to retrieve all available tools from the server
  • Returns result. tools containing tool definitions
  • Each tool includes a description and input schema
  • These definitions are passed to Claude

call_tool()

  • Calls self. session. call_tool(tool_name, tool_input)
  • Takes the tool name and input parameters provided by Claude
  • Executes the tool on the server and returns the result

Testing the Implementation

  • Use the testing harness at the bottom of the mcp_client. py file
  • Run with uv run mcp_client. py (or python mcp_client. py without UV)
  • Verify that tools are listed correctly and can be called
  • Once implemented, the CLI can request Claude to use these tools to read and edit documents

Takeaways

  • MCP client wraps a client session to manage the connection to the MCP server and handle resource cleanup
  • The client exposes server functionality by allowing other code to access tools and prompts defined by the MCP server
  • list_tools() retrieves available tools from the server via self. session. list_tools() and returns tool definitions with descriptions and input schemas
  • call_tool() executes a tool on the server by calling self. session. call_tool(tool_name, tool_input) with parameters provided by Claude
  • Test the implementation using the testing harness with uv run mcp_client. py to verify tools are listed and callable
Flashcards 9 cards
Question
click to reveal · ←/→
Answer
click to flip back
Export to Anki (.tsv) ↓
Knowledge check 1 questions