- Create a simple chatbot using Anthropic
- Connect to the Galileo MCP server to add tools to your chatbot
- Add logging with Galileo
Before you start
Before you begin, ensure you have:- Python 3.9+ installed
- A Galileo API key
- An Anthropic API key
Install dependencies
To use Galileo, you need to install some package dependencies, and configure environment variables.1
Install Required Dependencies
Install the required dependencies for your app. Create a virtual environment using your preferred method, then install dependencies inside that environment:
2
Create a .env file, and add the following values
This assumes you are using a free Galileo account. If you are using a custom deployment, then you will also need to add the URL of your Galileo Console:
.env
Create a simple chat bot with logging to Galileo
1
Create a project file
Create a new file called This code will log the interactions with the LLM to Galileo. Each time you run the application, a new session will be started, and each turn in the conversation will be a new trace.
app.py. Add the following code to this file to create a basic chatbot to interact with your chosen Anthropic model:2
Run the code
Run the code to ensure it works. Ask the chat bot a simple query and make sure you get a response.Then open your Log stream in Galileo, and you will see the queries logged as traces in a session.

Add tool calling against an MCP server to the chat bot
1
Create a file for the MCP client
Create a new file called This code defines an
mcp_client.py. Add the following code to this file to create an MCP client:MCPClient class that connects to the Galileo MCP server.2
Import the MCPClient
In your
app.py file, import the MCP Client, and create an instance of it. Add the following import statement to the top of the app.py:3
Create and initialize the MCP client
Under the declaration of the Anthropic client and message history, create an instance of the MCP client with the following code:Then in the
main function, connect the MCP client to the Galileo MCP server. Add the following line of code to the top of the main function:4
Pass the tools list to the LLM
The MCP client loads a list of tools from the MCP server. This needs to be passed to the LLM so that it can decided to call a tool if required.Change the Tools are required when a user query is passed to the LLM, but not when the LLM is processing the response from the tool call.The
call_llm function to take a boolean parameter called use_tools. Then if this is set, set the tools parameter on the call to anthropic.messages.create to use the tools from the MCP client.Change the call_llm function to the following:use_tools parameter allows the calling code to control if tools are used, and turn them off when the tool response is processed. You will set this up in a later step.5
Process a tool use request from the LLM
If the LLM returns a request to call a tool, you will then need to call the relevant tool, and pass the response from the tool back to the LLM.In the This code calls the MCP client to call the tool on the MCP server. The response is saved to the message history, then the message history is sent back to the LLM, this time without the tools list. The LLM then processes the tool result and returns a response.
for content in response.content: block, add another clause after the if content.type == "text": block for if the content type is tool use:6
Run the code
Run the code to ensure it works. Ask the chat bot a query that the Galileo MCP server can answer, such as “How do I use the GalileoLogger?”.When you run the app, a list of the all the tools on the server will be written to the console. Then if you ask a question that the MCP server can help with, the LLM will return a tool use request, the tool will be called, the tool result sent back to the LLM, then the final answer will be returned from the LLM and output to the console.
Log the tool call as a tool span
1
Capture the start time of the tool call
Add the following code before the call to
mcp_client.call_tool to get the start time of the call. This will allow you to time the tool call and add this duration to the span.2
Log the tool span
After the tool call, log a tool span with the input and output of the tool. Add the following code after the call to
mcp_client.call_tool:3
Run the code
Run the code and ask a question that will cause the tool to be called. Then open your Log stream in Galileo, and you will see the tool calls logged under the traces.
