MCPMCP Python SDK 2.2 · Python 3.10+
0%
1
Curious builder0 XP earned · 300 to level 2
0 daysFinish a lesson to begin
Badge collection0 of 6 unlocked
24 small wins to finish your pathNext lesson

Client: connecting and calling a tool

A client is the part of an application that speaks MCP to one server. The SDK's Client can also connect to a server object in memory, as this course does.

Example
import asyncio

from mcp import Client
from shop import mcp


async def main():
    async with Client(mcp) as client:
        result = await client.call_tool("lookup_order", {"order_id": "A17"})
        print(result.content[0].text)
        print(result.structured_content)
        print(result.is_error)


asyncio.run(main())

Client(mcp) is given the server object, so it connects in memory: no process to start, no port. The call still goes through the real protocol, listed, validated and run exactly as it would over a network. Lessons 15 and 16 connect to the same server in the two ways a real application does.

async with connects when the block starts and disconnects when it ends. Every client method is async, so it is awaited, inside main, which asyncio.run starts.

Three parts of a result

content is a list of blocks for the model to read; here, one text block. structured_content is the same result as data for the application's code; a string return is wrapped as {"result": ...}. is_error says whether the call failed, and lesson 6 is about when it is True.

What the connection knows

Example
import asyncio

from mcp import Client
from shop import mcp


async def main():
    async with Client(mcp) as client:
        print(client.server_info.name)
        print(client.protocol_version)
        print(client.server_capabilities.tools)
        print(client.server_capabilities.completions)


asyncio.run(main())

When a client connects, the two sides agree on a protocol version, and the server declares its capabilities: the kinds of request it will answer. MCPServer always declares tools, resources and prompts; resources arrive in lesson 8 and prompts in lesson 10. Completions, argument autocomplete, needs a handler this server does not have, so it is None, and a client will not ask for it.

Try it yourself
  • Call lookup_order for B42.
  • Print result itself, not its parts, to see every field.
  • Call a tool that does not exist, "cancel_order", and print is_error and the text. Lesson 6 explains the answer.

Slow is fine. Stopping is the only problem.