MCP and LLMs: Tools
1. MCP and LLMs: Tools
Now that you've created each of the three main primitives exposed by MCP servers, we can begin to integrate them in an LLM application. Let's start with tools.2. MCP Tools to LLM Tools
The exact process of hooking-up MCP servers with client-side LLMs is highly dependent on which model provider you're using. Different providers may accept tools in different ways and with different syntax, so it's important to look at how your preferred provider and model accepts them. In this video, we'll use an OpenAI LLM and their Responses API, which is designed with tool-calling in-mind. The important thing here isn't memorizing the exact code, but instead focusing on the key processes in the workflow. With that, let's dive in!3. The Tool-Calling Workflow
Tool-calling is a five-step process:4. The Tool-Calling Workflow
a request is made to the model that contains all the tool definitions and the user's message. In this case, we'll send one tool definition for our currency converter tool.5. The Tool-Calling Workflow
Then, if the model decides the user's message requires this tool, it creates a tool call with the arguments to pass to the function, extracted from the user's message.6. The Tool-Calling Workflow
These arguments are then passed to the tool function and executed,7. The Tool-Calling Workflow
and the results are passed back to the model in a second request, so it now knows what the converted datetime is.8. The Tool-Calling Workflow
Finally, it's now able to respond to the user's message. Let's begin to code this out.9. MCP Server: timezone_server.py
Our MCP server is still available in a script called timezone_server.py,10. Expanding the Client Code
and the client functions you wrote in Chapter 1 to list and call MCP tools are available on the client-side. We'll be expanding this client-side code with an additional function to call the LLM on a user query.11. Setup: Format the Tools for the LLM
Before we begin the tool-calling workflow, we need to find out what tools are available and format them for the LLM. We use our function to list the server's tools, then loop over them and define a new dictionary for each one. This dictionary form is required by OpenAI's Responses API. We extract the tool name, its description if present, and a JSON schema of its parameters. Finally, we append each tool dictionary to the openai_tools list.12. 1. Send the Query and Tools to the LLM
Then we begin by sending the tools list and user query to the LLM, so it can decide whether a tool call is required. We start by instantiating an asynchronous client for communicating with the OpenAI API, which will allow us to maintain our "await"/"async with" workflow. Then we can call client.responses.create() to create a request to a tool-calling OpenAI model, passing the query as the model input and the openai_tools list to the tools argument.13. 2. Checking for a Tool Call
Finally, we can extract the first item back from the model so we can check if it's a tool call. We can check if a function call is made by checking the item's "type" attribute. If it is, we can extract the tool arguments requested by the LLM and the name of the tool it called.14. 3. Calling the Tool | 4. The Follow-Up Message
In the next step, we use the call_mcp_tool() function we defined to call the tool chosen by the LLM with the arguments it determined. From here, we can send a follow-up message to the model, containing the user's original query, the LLM's first message, and now adding the result from the tool call as a third message.15. 5. The Final Response
Finally, we can print the final follow-up message. If a tool wasn't requested, i.e., a function call wasn't made in the first request, we just return the model's response. This is what would happen if the user query didn't require or couldn't be addressed by tool calls.16. Testing the Function
We can test our workflow with a simple query, and now the LLM has access to the timezone information.17. The Tool-Calling Workflow
We covered a lot here! Just remember that calling tools is a five-step process. The only difference with MCP is that these tools are server-side rather than client-side.18. Let's practice!
Time to give this a go!Create Your Free Account
or
By continuing, you accept our Terms of Use, our Privacy Policy and that your data is stored in the USA.