MCP Learning Series - Part 4

Now that we have an idea on what is MCP and how to create a simple MCP server and add it to AI Application like Claude.

But, Do we really need a AI Application to access MCP Server?

Answer is NO

AI Applications like Claude are using LLM for thinking, reasoning and acting. 

MCP is a Client - Server architecture.

Where the client is your AI application. But, to be precise these AI Applications are called "MCP Host".

These MCP host contains inbuild MCP client.


The above diagram explains the architecture of MCP.

1) After we add the weather MCP server to the AI Application - MCP Host.

2) MCP Client creates a dedicated connection (1:1) with the weather MCP Server.

3) This connection exist, regardless of the MCP Server is used or not.

4) When the user enters the prompt, "What is the weather in Chennai?"

5) The LLM discovers the tools available and based on the input context and tool description. It uses the weather tool.

6) This call is forwarded from the MCP Host (LLM) -> MCP Client -> MCP Server (Weather)

7) The MCP Server responds back to the MCP Client.

8) The MCP Client forwards to LLM.

9) LLM responds back to the user.


Key points:

The LLM never talks to the server directly.

The LLM chooses the tool from the descriptions.

There can be multiple rounds - Model may call the tool twice before answering.

No tool is called if it isn’t needed.

The connection is typically already open.

Sample MCP Client: To access the MCP Server without AI Application

import asyncio
import sys

from mcp import ClientSession, StdioServerParameters
from mcp.client.stdio import stdio_client


async def main():
params = StdioServerParameters(command=sys.executable, args=["main.py"])

async with stdio_client(params) as (read, write):
async with ClientSession(read, write) as session:
await session.initialize()

#result = await session.list_tools()
#for tool in result.tools:
# print(tool.name, "-", tool.description)
# print(" schema:", tool.inputSchema)
result = await session.call_tool(
"search_news",
{"query": "Anthropic", "max_results": 3, "lang": "en"},
)

for item in result.content:
if item.type == "text":
print(item.text)


asyncio.run(main())


Comments

Popular posts from this blog

Agentic AI - Series 3

Agentic AI - Series 4

Principal SRE - Interview Question