Skip to main content

Activity: Create a first MCP server

What a server can do

An MCP Server is a program that exposes specific capabilities through the MCP protocol. It can be a simple script or a complex application, depending on the use case. The server can interact with local data sources, remote services, and other tools to provide context to LLMs.

For example, an MCP server can:

  • Access local files and databases
  • Connect to remote APIs
  • Perform computations
  • Integrate with other tools and services
  • Provide a user interface for interaction

Creating a simple server

To create a server, you need to follow these steps:

  • Install the MCP SDK.
  • Create a new Node.js project and set up the project structure.
  • Write the server code.
  • Test the server.

-1- Install the MCP SDK

note

Make sure you have installed Node.js before running the code below. You can check if you have it installed by running node -v in your terminal.

  1. Create a virtual environment
python -m venv venv
venv\Scrips\activate
  1. Install dependencies
pip install "mcp[cli]"

-2- Create a new project

Create the project structure by following these steps:

  1. Create a src folder.

  2. Create a file server.py in it.

-3- Create the server

  1. Create the server by adding the below code:
# server.py
from mcp.server.fastmcp import FastMCP

# Create an MCP server
mcp = FastMCP("Demo")

  1. Now, let's add the tools:
# Add an addition tool
@mcp.tool()
def add(a: int, b: int) -> int:
"""Add two numbers"""
return a + b

  1. Finally, let's add a resource:
 # Add a dynamic greeting resource
@mcp.resource("greeting://{name}")
def get_greeting(name: str) -> str:
"""Get a personalized greeting"""
return f"Hello, {name}!"

if __name__ == "__main__":
mcp.run()

Testing our server

So far, you've created a simple MCP server and your file directory structure should look like this:

src/
|-- server.py

Your server has a tool "add" and a resource "greeting". The server is ready to receive messages on stdin and send messages on stdout.

-1- Run the inspector tool

The easiest way to test your server is to use the inspector tool. It's a tool we can run via npx.

mcp dev server.py

You should see a window like this:

Connect

-2- Connect to the server

  • Select to "Connect" and you should see the window below:

    Connect

  • Select "List tools", to see what tools are available:

    Connect

-3- Run the tool

  • Select "add" and a dialog on your right will ask you to fill in the parameters:

    Connect

  • You should see the result of the tool in the inspector, see 16 in the bottom result:

    Connect

Congrats, you've managed to create a simple MCP server and run the inspector tool to test it!

You're ready for your next challenge, creating a client that can call the server and use the tools and resources you've created.

-4- Summary

You've learned to build a simple MCP Server and test it using the inspector tool. To look at a working solution you can clone the below:

git clone https://github.com/softchris/mcp-workshop-mcp.git
cd mcp-workshop