Key Concepts
- Model Context Protocol (mCP)
- Server Tools vs. Client Tools
- mCP Server and mCP Client
- Tools Definition and Execution
- Message Flow in LLM Interactions
- Server-Sent Events (SSE) Transport
- Standard IO Transport
- System Prompt Engineering
- Max Steps in Tool Calls
E-commerce Application Overview
The video focuses on enhancing an e-commerce application using the Model Context Protocol (mCP) to integrate Large Language Models (LLMs) with backend services. The application consists of:
- Frontend: A TanStack Start application on port 3000, providing the user interface, including an AI assistant.
- Products API: An Express app on port 882, responsible for managing product information (guitars in this example).
- Fulfillment API: An Express app on port 880, handling order processing.
- Admin App: A TanStack Start app on port 3001, used for monitoring fulfilled orders and inventory.
The initial problem is that the AI assistant in the frontend doesn't have access to the Products API, leading to irrelevant recommendations.
Connecting AI Assistant to Products API using Server Tools
The solution involves using "server tools" to enable the LLM to access the Products API. The message flow is as follows:
- The user inputs a query (e.g., "recommend a techy guitar") into the AI assistant.
- The frontend sends the query to the server.
- The server sends the query to the LLM, along with a "tools definition" (metadata about available tools, such as
getProducts). - The LLM determines that it needs product information and requests the server to execute the
getProductstool. - The server calls the Products API (port 882) and retrieves the product data.
- The server adds the product data (as JSON) to the message history as the result of the tool call.
- The server sends the updated message history to the LLM.
- The LLM analyzes the data and provides a relevant recommendation (e.g., "motherboard guitar").
Implementation Details:
- The
AI tools.tsfile defines the available tools using thetoolfunction from the Vercel AI Library. - The
getProductstool is defined with a description ("get all the products from the database"), no parameters (using Zod for schema definition), and anexecutefunction that calls thefetchGuitarshelper function. - The
ai.tsfile uses Vercel AI'sstreamTextfunction to communicate with the LLM (Anthropic in this case). - The
streamTextfunction takes the model, messages, a system prompt ("You are an AI for a music store"), and the tools as arguments. maxStepsparameter is crucial for tool calls, as it defaults to 1. Tool calls often require multiple steps (requesting the tool, then analyzing the data), somaxStepsshould be set to a value greater than 1.
System Prompt Engineering:
The system prompt can be tweaked to encourage the LLM to use the tools. For example, adding "There are products available for purchase. You can recommend a product to the user" and explicitly mentioning the getProducts tool.
Client Tools
Client tools are executed on the client-side (browser). mCP is not suitable for client tools because mCP servers typically don't have direct access to the client environment.
Example:
A recommendGuitar client tool could take a guitar ID as a parameter and display a special card on the UI with details about that guitar.
Implementation:
- A
recommendGuitartool is defined inAI tools.tswith anidparameter (using Zod). - The
useChathook in the frontend handles theonToolCallevent forrecommendGuitar. - The
messagescomponent in the UI checks for tool invocations of typerecommendGuitarand renders aGuitarRecommendationcomponent with the provided ID.
Integrating with Fulfillment API using mCP Server
To enable the AI assistant to make orders, an mCP server is introduced to act as an intermediary between the frontend and the Fulfillment API.
Flow:
- The user asks the AI assistant to order a product.
- The frontend sends a tool call request (
orderProduct) to the server. - The server forwards the tool call request to the mCP order server.
- The mCP order server interacts with the Fulfillment API (port 880) to place the order.
- The mCP order server sends the result back to the server, which then relays it to the frontend.
mCP Server Implementation:
- An mCP order server is created using the
@model-context/protocolSDK, Zod, and Express. - The server defines tools like
getInventory(to check product availability) andpurchase(to place an order). - The
purchasetool takes parameters likeitems(cart items with quantity) andcustomerName. - The server listens on port 881 and exposes two endpoints:
/sse(for establishing a stateful connection using Server-Sent Events) and/sse/messages(for posting messages and receiving responses).
mCP Client Implementation:
- The frontend uses the
experimentalCreateMCPClientfunction from the@model-context/protocolSDK to connect to the mCP order server. - The
sseEndpointis set tolocalhost:881. - The tools from the mCP client are added to the list of available tools in the frontend.
- The system prompt is updated to instruct the AI to use
getInventoryandpurchase.
Transports:
The video demonstrates two transport mechanisms for mCP:
- Server-Sent Events (SSE): A unidirectional protocol where the server pushes updates to the client.
- Standard IO: Used for connecting Claude to the mCP order server.
Business Intelligence with Claude and Standard IO
The video demonstrates how to connect Claude to the mCP order server using Standard IO for business intelligence purposes.
Steps:
- The server logic (tool definitions and execution) is extracted into a separate file (
serverLogic.js). - Two server instances are created: an SSE server and a Standard IO server.
- The Standard IO server uses the
StandardIOServerTransportfrom the@model-context/protocolSDK. - In Claude's settings, the mCP server is configured with the command
nodeand the path to the Standard IO server script. - A new tool,
getOrders, is added to the server logic to allow Claude to access order data. - Claude is restarted to load the new configuration.
- Claude is prompted to analyze the orders and generate a graph of sales projections.
- Claude calls the
getOrderstool, retrieves the order data, and generates JavaScript code to create a bar chart of sales data.
Conclusion
The video provides a detailed walkthrough of integrating LLMs with backend services using the Model Context Protocol. It covers server tools, client tools, mCP server and client implementation, different transport mechanisms (SSE and Standard IO), and demonstrates how to use mCP for both functional tasks (placing orders) and business intelligence (analyzing sales data). The key takeaway is that mCP provides a flexible and scalable way to connect LLMs to various data sources and services, enabling more powerful and context-aware AI applications.
AI summaries can miss context or contain errors. Check important details against the original video.