DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Related

  • Production Checklist for Tool-Using AI Agents in Enterprise Apps
  • Understanding MCP Architecture: LLM + API vs Model Context Protocol
  • Open-Source LLM Tools Worth Your Time
  • MCP vs Skills vs Agents With Scripts: Which One Should You Pick?

Trending

  • The ORM Is Over: AI-Written SQL Is the New Data Access Layer
  • Introduction to Tactical DDD With Java: Steps to Build Semantic Code
  • Smart Deployment Strategies for Modern Applications
  • DevOps Is Dead, Long Live Platform Engineering
  1. DZone
  2. Data Engineering
  3. AI/ML
  4. How to Build an MCP Server

How to Build an MCP Server

Learn what Model Context Protocol (MCP) is and isn’t, how MCP clients and servers work, and how to build and test an MCP server locally using Python.

By 
Kumar Abhishek user avatar
Kumar Abhishek
·
Feb. 13, 26 · Tutorial
Likes (3)
Comment
Save
Tweet
Share
2.1K Views

Join the DZone community and get the full member experience.

Join For Free

Model Context Protocol has been playing a crucial role in integrating various tools with agents in a very streamlined manner. You can expose your tools via APIs and connect to the MCP clients. At the same time, there has been lots of confusion about MCP.  Below clarifies the doubt: 

What MCP Server Is Not

  • MCP is not a framework for building agents.
  • MCP is not a Python library.
  • MCP is not a container.
  • MCP is not a way to code agents.

How Model Context Protocol (MCP) Works

Ultimately, 

  • Model Context Protocol (MCP) is a protocol — it’s a standard.
  • It is a simple way to integrate tools, resources, and prompts.

The diagram below shows how the ecosystem works.


The host represents the IDE where your MCP client is residing. The MCP client connects with the MCP Server in a 1:1 way. This means that one MCP client will connect to one MCP server only. It might be the case that you have multiple tools exposed on one MCP server.  

The MCP server can connect to the resources in two ways: 

  1. STDIO
  2. Server-Sent Event (SSE) 

STDIO is the mechanism for connecting any MCP client to the MCP server when the client and server are on the same network. In a certain scenario, when you are using a tool exposed on an MCP server built by a third-party open-source provider. In this case, you will need to use SSE to connect to the MCP server.

In both scenarios, only the MCP client source code differs.

The diagram below shows a typical workflow in which a relevant answer to a chatbot query is obtained using a tool available on any MCP server.

Simplified MCP sequence diagram


In the above sequence diagram, the flow is like below in a sequential manner:

  1. User enters a query in the chatbot UI
  2. Agentic AI application receives the user query and routes the query to the Bedrock LLM Model
  3. LLM model processes the user query, and with the help of System Prompt, it decides which tool to invoke to process the user’s query
  4. LLM model sends the tool details to the AI Application
  5. AI Application has an MCP Client, which translates the Tool Use response as per the MCP Protocol
  6. MCP Client routes the request to the MCP Server
  7. MCP Server executes the tool as per the request and sends the result back to the MCP Client
  8. MCP Client receives the tool result
  9. MCP Client sends the tool result to the Bedrock LLM model to format the result in a presentable format
  10. LLM model sends the result to the AI Application
  11. AI Application sends the result to the end user

If you want to try to build an MCP server on your laptop, you will need to do the following and ultimately validate your local MCP Server tools. 

Set up the Python environment. Below are the steps:

1. Create a Python virtual environment:

Shell
 
uv init my-mcp-server
cd my-mcp-server
uv venv
source .venv/bin/activate  # or .venv\Scripts\activate on Windows


2. Add dependencies (Python libraries) for MCP:

Shell
 
uv add mcp


 

3. Now, you need to instantiate FastMCP in your Python code as below:

Python
 
from mcp import FastMCP
mcp = FastMCP(name="My_MCP_server")

  

4. Then you need to write Python functions that you would like to expose as tools using MCP. So, write Python functions and expose them as MCP tools. In the example below, I wrote maths.py, which contains three functions (add, subtract, and multiply). 

Below is a glimpse of Python functions written to perform mathematical actions on two numbers:

Python functions written to perform mathematical actions

 

 

Below is the equivalent MCP Server code for the same mathematical functions above:

Note that all Python functions are bind my annotation @mcp.tool().

  1. Expose data as an MCP resource. Note the @mcp.resource annotation used in the get_my_data() function.
  2.  Execute MCP Server script on terminal: uv run maths_server.py.

Test the MCP Server using the MCP Inspector. Use the command below to start the MCP Inspector:

Shell
 
npx @modelcontextprotocol uv run <file_path>\\maths_server.py


See the diagram below:

Diagram

 

Once you see the above message in the terminal, click on the MCP Inspector link as highlighted in the red block. You can see the Inspector screen in the browser.   

Now, you can validate your tool in your MCP Server, built in your local laptop's Python environment.

  1. First, select STDIO as your connectivity mechanism between the MCP Client and Server. Click on Connect.

     

  2. Once connected, navigate to the Tools tab.
  3.  Click on the List Tools. You can view your functions as tools in the MCP server. You can validate the result published by your tool, as done above for the add tool.

Model Context Protocol Inspector is an efficient way to validate tools exposed on your MCP Server. Once validated on your local Python environment on your own laptop, you can push these source code files (maths.py and maths_server.py) to the cloud native Git repository and build the tool on your MCP server in the cloud environment. You can even Dockerize the MCP Server tool and deploy it on Kubernetes.   

In my next article, I will mention steps to Dockerize your MCP Server.  

Tool large language model

Opinions expressed by DZone contributors are their own.

Related

  • Production Checklist for Tool-Using AI Agents in Enterprise Apps
  • Understanding MCP Architecture: LLM + API vs Model Context Protocol
  • Open-Source LLM Tools Worth Your Time
  • MCP vs Skills vs Agents With Scripts: Which One Should You Pick?

Partner Resources

×

Comments

The likes didn't load as expected. Please refresh the page and try again.

  • RSS
  • X
  • Facebook

ABOUT US

  • About DZone
  • Support and feedback
  • Community research

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Core Program
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 215
  • Nashville, TN 37211
  • [email protected]

Let's be friends:

  • RSS
  • X
  • Facebook