๐ What is MCP (Model Context Protocol)?
MCP (Model Context Protocol) is a standard that allows AI models to interact with external tools and services in a structured way.
It was introduced by Anthropic to make AI assistants more powerful and modular.
Instead of hardcoding logic in your backend, MCP allows you to expose tools that the AI can discover and use dynamically.
Traditional AI vs MCP-Based AI
Traditional AI Integration
In traditional applications, developers manually route user requests to APIs.
Example:
if (question.includes("weather")) {
getWeather()
}
else if (question.includes("bitcoin")) {
getCryptoPrice()
}
else if (question.includes("joke")) {
getJoke()
}
Problems with this approach
โ Hardcoded logic
โ Difficult to scale
โ AI cannot decide which tool to use
โ Adding new tools requires modifying backend code
MCP-Based AI Architecture
With MCP, tools are exposed to the AI model.
The AI decides when to use them.
Architecture:
User โ AI Model โ MCP Client โ MCP Server โ Tools (APIs, services)
This approach allows us to build dynamic AI agents.
Many modern AI tools follow this architecture including:
- Cursor IDE
- Claude Desktop
๐ง Our AI Agent Architecture
Our project contains two main components:
project โ client.js โ server.js โ package.json โ .env
server.js
Defines the tools.
client.js
Acts as the AI agent that discovers and calls tools.
๐ Tools We Will Build
Our MCP server exposes four tools.
1๏ธโฃ Crypto Price Tool
Fetches cryptocurrency prices using CoinGecko API.
Example output:
๐ฐ BITCOIN Price: $68124 24h Change: 1.72%
2๏ธโฃ Weather Tool
Fetches weather using Open-Meteo API.
Advantages:
- Free
- No API key required
- Fast
Example output:
๐ค Weather in London Temperature: 18ยฐC Wind Speed: 10 km/h
3๏ธโฃ Random Joke Tool
Uses the Official Joke API to fetch random programming jokes.
Example output:
๐ Joke Why do programmers prefer dark mode? Because light attracts bugs.
4๏ธโฃ UUID Generator Tool
Generates unique identifiers using the uuid library.
Example:
๐ UUID 9c37c0aa-1b4e-4c7e-a437-29b8cfa7b7d4
UUIDs are commonly used for:
- database IDs
- session tokens
- unique transaction references
โ๏ธ Step 1 โ Install Dependencies
First install the required packages:
npm install @modelcontextprotocol/sdk openai axios uuid zod dotenv
These packages allow us to:
- Create MCP servers
- Connect AI models
- Fetch API data
- Validate inputs
๐งฉ Step 2 โ Create the MCP Server
The MCP server defines and exposes tools.
Each tool includes:
- name
- description
- input schema
- execution function
Example structure:
server.registerTool( tool_name, configuration, execution_function )
Once registered, the tool becomes available to the AI.
๐ค Step 3 โ Build the AI Agent
The AI agent connects to the MCP server.
Client โ MCP Server
We use:
StdioClientTransport
This allows communication between client and server using standard input/output streams.
๐ Step 4 โ Automatic Tool Discovery
The AI agent dynamically fetches available tools:
client.listTools()
Output example:
get_crypto_price get_weather get_joke generate_uuid
This means if we add more tools later, the agent will automatically discover them.
๐ง Step 5 โ Send Tools to the AI Model
We convert MCP tools into the format required by the AI model.
The AI model we use is GPTโ4o mini.
We send the tool definitions to the model so it understands:
what tools exist what parameters they require when they should be used
โก Step 6 โ AI Chooses the Tool
When a user asks:
weather in London
The AI returns a tool call.
Example:
get_weather
{
city: "London"
}
The client then executes that tool through the MCP server.
๐ Full System Flow
Here is the complete flow:
User Question โ AI Model โ Tool Selection โ MCP Client โ MCP Server โ API Tool Execution โ Response Returned
This design allows AI systems to scale easily.
๐งช Example Queries
You can ask the AI agent:
bitcoin price weather in tokyo tell me a joke generate uuid
The AI automatically selects the correct tool.
๐ Why MCP Is Powerful
Using MCP provides several advantages:
Dynamic Tool Discovery
No hardcoding required.
AI-Driven Decisions
The model decides which tool to call.
Scalable Architecture
You can easily add new tools like:
- database search
- file system access
- GitHub integrations
- web search
Without modifying the AI logic.
๐ง Final Thoughts
MCP is changing the way we build AI systems.
Instead of building rigid chatbot pipelines, developers can now build flexible AI agents capable of interacting with real-world tools.
This architecture is becoming the foundation of next-generation AI assistants.
