Constructing a Easy MCP Server in Python

Date:

🚀 Able to supercharge your AI workflow? Strive ElevenLabs for AI voice and speech technology!

On this article, you’ll be taught what Mannequin Context Protocol (MCP) is and the right way to construct a easy, sensible task-tracker MCP server in Python utilizing FastMCP.

Subjects we are going to cowl embody:

  • How MCP works, together with hosts, purchasers, servers, and the three core primitives.
  • implement MCP instruments, sources, and prompts with FastMCP.
  • run and take a look at your MCP server utilizing the FastMCP consumer.

Let’s not waste any extra time.

Building Simple MCP Server Python

Constructing a Easy MCP Server in Python
Picture by Editor

Introduction

Have you ever ever tried connecting a language mannequin to your individual information or instruments? In that case, it usually means writing customized integrations, managing API schemas, and wrestling with authentication. And each new AI utility can really feel like rebuilding the identical connection logic from scratch.

Mannequin Context Protocol (MCP) solves this by standardizing how massive language fashions (LLMs) and different AI fashions work together with exterior methods. FastMCP is a framework that makes constructing MCP servers easy.

On this article, you’ll be taught what MCP is, the way it works, and the right way to construct a sensible job tracker server utilizing FastMCP. You’ll create instruments to handle duties, sources to view job lists, and prompts to information AI interactions.

You will get the code on GitHub.

Understanding the Mannequin Context Protocol

As talked about, Mannequin Context Protocol (MCP) is an open protocol that defines how AI functions talk with exterior methods.

How MCP Works

MCP has three elements:

Hosts are the AI-powered functions customers truly work together with. The host might be Claude Desktop, an IDE with AI options, or a customized app you’ve constructed. The host comprises (or interfaces with) the language mannequin and initiates connections to MCP servers.

Shoppers connect with servers. When a number wants to speak to an MCP server, it creates a consumer occasion to handle that particular connection. One host can run a number of purchasers concurrently, every related to a unique server. The consumer handles all protocol-level communication.

Servers are what you construct. They expose particular capabilities — database entry, file operations, API integrations — and reply to consumer requests by offering instruments, sources, and prompts.

So the consumer interacts with the host, the host makes use of a consumer to speak to your server, and the server returns structured outcomes again up the chain.

To be taught extra about MCP, learn The Full Information to Mannequin Context Protocol.

The Three Core Primitives

MCP servers expose three varieties of performance:

Instruments are features that carry out actions. They’re like executable instructions the LLM can invoke. add_task, send_an_email, and query_a_database are some examples of instruments.

Sources present read-only entry to information. They permit viewing data with out altering it. Examples embody lists of duties, configuration recordsdata, and consumer profiles.

Prompts are templates that information AI interactions. They construction how the mannequin approaches particular duties. Examples embody “Analyze these duties and counsel priorities” and “Assessment this code for safety points.”

In observe, you’ll mix these primitives. An AI mannequin may use a useful resource to view duties, then a device to replace one, guided by a immediate that defines the workflow.

Setting Up Your Atmosphere

You’ll want Python 3.10 or later. Set up FastMCP utilizing pip (or uv should you favor):

Let’s get began!

Constructing a Process Tracker Server

We’ll construct a server that manages a easy job record. Create a file referred to as task_server.py and add the imports:

These give us the FastMCP framework and datetime dealing with for monitoring when duties had been created.

Initializing the Server

Now arrange the server and a easy in-memory storage:

Right here’s what this does:

  • FastMCP("TaskTracker") creates your MCP server with a descriptive identify.
  • duties is an inventory that shops all duties.
  • task_id_counter generates distinctive IDs for every job.

In an actual utility, you’d use a database. For this tutorial, we’ll preserve it easy.

Creating Instruments

Instruments are features embellished with @mcp.device(). Let’s create three helpful instruments.

Device 1: Including a New Process

First, let’s create a device that provides duties to our record:

This device does the next:

  • Takes a job title (required) and an elective description.
  • Creates a job dictionary with a singular ID, standing, and timestamp.
  • Provides it to our duties record.
  • Returns the created job.

The mannequin can now name add_task("Write documentation", "Replace API docs") and get a structured job object again.

Device 2: Finishing a Process

Subsequent, let’s add a device to mark duties as full:

The device searches the duty record for an identical ID, updates its standing to “accomplished”, and stamps it with a completion timestamp. It then returns the up to date job or an error message if no match is discovered.

Device 3: Deleting a Process

Lastly, add a device to take away duties:

This device searches for a job, removes it from the record, and returns affirmation with the deleted job information.

These three instruments give the mannequin create, learn, replace, and delete (CRUD) operations for job administration.

Including Sources

Sources let the AI utility view information with out modifying it. Let’s create two sources.

Useful resource 1: Viewing All Duties

This useful resource returns the whole job record:

Right here’s how this works:

  • The decorator @mcp.useful resource("duties://all") creates a useful resource with a URI-like identifier.
  • The perform codecs all duties into readable textual content with emojis for visible readability.
  • It returns a easy message if no duties exist.

The AI utility can learn this useful resource to know the present state of all duties.

Useful resource 2: Viewing Pending Duties Solely

This useful resource filters for incomplete duties:

The useful resource filters the duty record all the way down to pending gadgets solely, codecs them for simple studying, and returns a message if there’s nothing left to do.

Sources work nicely for information the mannequin must learn continuously with out making adjustments.

Defining Prompts

Prompts information how the AI utility interacts along with your server. Let’s create a useful immediate:

This immediate defines a structured template for job evaluation, tells the AI what data to incorporate, and references the useful resource to make use of for information.

Prompts make AI interactions extra constant and helpful. When the AI mannequin makes use of this immediate, it is aware of to fetch job information and analyze it on this particular format.

Working and Testing the Server

Add this code to run your server:

Begin the server out of your terminal:

You’ll see output confirming the server is working. Now the server is able to settle for connections from MCP purchasers.

Testing with the FastMCP Consumer

You possibly can take a look at your server utilizing FastMCP’s built-in consumer. Create a take a look at file referred to as test_client.py and run it:

You’ll see your instruments execute and sources return information. This confirms every part works appropriately.

Subsequent Steps

You’ve constructed a whole MCP server with instruments, sources, and prompts. Right here’s what you are able to do to enhance it:

  • Add persistence by changing in-memory storage with SQLite or PostgreSQL.
  • Add instruments to filter duties by standing, date, or key phrases.
  • Construct prompts for precedence evaluation or job scheduling.
  • Use FastMCP’s built-in auth suppliers for safe entry.

Begin with easy servers like this one. As you develop extra comfy, you’ll end up constructing helpful MCP servers to simplify extra of your work. Pleased studying and constructing!

🔥 Need one of the best instruments for AI advertising? Take a look at GetResponse AI-powered automation to spice up your online business!

spacefor placeholders for affiliate links

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Share post:

Subscribe

spacefor placeholders for affiliate links

Popular

More like this
Related

Which must you use? [2026]

🤖 Increase your productiveness with AI! Discover Quso: all-in-one...

Social Media Burnout: How one can Keep away from Spreading Your self Too Skinny

🚀 Automate your workflows with AI instruments! Uncover GetResponse...

Past Accuracy: 5 Metrics That Really Matter for AI Brokers

🚀 Able to supercharge your AI workflow? Strive...

Authorship Launches in Docs with Brokers, Creating Extra Transparency and Higher Experiences

🤖 Increase your productiveness with AI! Discover Quso: all-in-one...