Your First MCP Client
30%
Prototyping an MCP Client
Start by creating a new folder for the client and a new Python file:
mkdir -p $HOME/workspace/mcp-client
cd $HOME/workspace/mcp-client
Use uv to create a new project and install the MCP SDK:
# Create a new uv project with Python 3.12
uv init --bare --python 3.12
# Add the MCP SDK and OpenAI client as dependencies
uv add mcp==1.26.0 openai==2.21.0
Once the installation is done, we create a new file called client.py and add the code for our MCP client. This is the command we'll run:
cat <<EOF > client.py
import asyncio
import json
import os
import sys
from mcp import ClientSession
from mcp.client.streamable_http import streamable_http_client
from openai import OpenAI
async def main():
if len(sys.argv) < 2:
sys.exit("Usage: python client.py " )
query = sys.argv[1]
openai = OpenAI(api_key=os.getenv("OPENAI_API_KEY"))
model = "gpt-5-mini"
url = "http://127.0.0.1:8000/mcp"
# Connect to the MCP server using the streamable HTTP transport
async with streamable_http_client(url) as (read, write, _):
# Create an MCP client session
async with ClientSession(read, write) as session:
# Initialize the session (handshake with the server)
await session.initialize()
# Discover available tools from the MCP server
available_mcp_tools = (await session.list_tools()).tools
# Covert MCP tools to OpenAI tool format
tools = [
{
"type": "function",
"function": {
"name": mcp_tool.name,
"description": mcp_tool.description,
"parameters": mcp_tool.inputSchema,
},
}
for mcp_tool in available_mcp_tools
]
# Define the initial messages for the chat completion
messages = [{"role": "user", "content":Practical MCP with FastMCP & LangChain
Engineering the Agentic ExperienceEnroll now to unlock current content and receive all future updates for free. Your purchase supports the author and fuels the creation of more exciting content. Act fast, as the price will rise as the course nears completion!
