How-to

How to build an MCP client in Python

Truffle

Truffle

Your codebase, in plain English

· 6 min read

Adjusting settings and connections

An MCP client connects to a server, discovers its tools, and calls them. With fastmcp the mechanics are a few lines — the interesting work is what you do with the tools once you have them.

1. Install

pip install fastmcp

2. Connect, list, call

import asyncio
from fastmcp import Client

async def main():
    async with Client("https://example.com/mcp") as client:
        tools = await client.list_tools()
        print([t.name for t in tools])
        result = await client.call_tool("get_forecast", {"city": "Amsterdam"})
        print(result)

asyncio.run(main())

For a token-protected server (like Truffle), pass your credential — fastmcp treats a plain string as a bearer token: Client(url, auth="trfl_your_token").

3. Hand the tools to a model

A real client feeds that tool list to an LLM, lets the model choose a tool, relays the call, and returns the result into the answer. When you expose many servers, rank tools against the question so you don't overwhelm the model — that pre-selection is the part worth getting right.

Read-only by default

Decide up front which tools a model may call, and keep anything that mutates data behind an explicit opt-in. That single rule is what makes connecting arbitrary servers safe. It's exactly how Truffle behaves as a client — see What is an MCP client?

Want me answering questions for your team?

Get started for free and install Truffle to your chat environment today.

Get started for free