Building a Context Sharing System using MCP (original) (raw)

Last Updated : 16 Apr, 2026

Model Context Protocol (MCP) is an open protocol developed by Anthropic that standardizes how applications share contextual information with Large Language Models (LLMs). Think of MCP as a universal connector or “USB port” for AI applications that helps them to exchange and use contextual data seamlessly. In this article we will make a project in which:

Step 1: Install Libraries

We will install the required libraries we will need.

pip install mcp[cli]
pip install asyncio

`

Step 2: Setting Up Project Directory Structure

We will create our project folder, organize files in similar structure as this maintains hierarchy. The commands to create folders are:

This sets up a clean, organized folder structure for the project.

PowerShell `

mkdir mcp-context-sharing cd mcp-context-sharing mkdir src mkdir src\server

Bash

mkdir -p mcp-context-sharing/src/server cd mcp-context-sharing

`

We will create our MCP server inside the main.py within the src/server folder.

Step 3: Create MCP Server

Open the main.py file in any of the IDE or editor and edit that file.

import asyncio from mcp import MCPServer

server = MCPServer() context_store = {}

`

2. Define MCP Tools

MCP tools are functions annotated with @server.tool(), making them remotely callable. These run asynchronously due to async def.

**Tool 1: share_context

@server.tool() async def share_context(agent: str, content: str): """ Store context from a specified agent. """ if agent not in context_store: context_store[agent] = [] context_store[agent].append(content) return {"result": f"Context stored from {agent}: {content}"}

`

**Tool 2: retrieve_context

@server.tool() async def retrieve_context(query: str): """ Retrieve all stored context entries matching the query text. """ matching = [] for agent, contents in context_store.items(): for content in contents: if query.lower() in content.lower(): matching.append({"agent": agent, "content": content}) if not matching: return {"result": f"No context found matching query: {query}"} return {"result": matching}

`

**Tool 3: list_all_context

@server.tool() async def list_all_context(): """ Return the full context store with contexts from all agents. """ return context_store

`

2. Starting the MCP Server

When the script runs, it:

if name == "main": print("MCP server started and waiting for clients...") server.serve()

`

Step 4: Update the settings.json for MCP Inspector

The settings.json was updated to configure VS Code’s MCP Inspector so it knows how to launch and communicate with the MCP server. Specifically, it tells the Inspector:

We are using VS Code for editing our project,

{ "servers": [ { "name": "Local MCP Server", "command": "python", "arguments": ["src/server/main.py"], "transport": "stdio" } ] }

`

Step 5: Starting MCP Server

After saving the server file, we will start the server,

cd C:\Path\To\mcp-context-sharing python src/server/main.py

Bash

cd /path/to/mcp-context-sharing python src/server/main.py

`

Step 6: Running MCP Server

After the MCP Server starts, on a new we type commands to open the MCP Inspector GUI.

cd C:\Path\To\mcp-context-sharing & "C:\Users\YOUR_USERNAME\AppData\Roaming\Python\Python313\Scripts\mcp.exe" dev src/server/main.py

Bash

cd /path/to/mcp-context-sharing mcp dev src/server/main.py

`

**Output:

Screenshot-2025-08-25-144431

Running MCP Server

Step 7: MCP Inspector GUI

After successfully running the commands, the GUI opens on localhost server automatically, if not then we can paste the following link to open the GUI:

Text `

http://localhost:3000

`

In the GUI, we can see various tabs.

MCP-Inspector-GUI

MCP Inspector GUI

Step 8: Changes in Connection Tab

In the Inspector we make certain changes:

connection

Server Connection

After the successful connection, it can be confirmed by the Connected notification.

Screenshot-2025-08-21-123029

Menu

2. We Select the Tools Tab

Screenshot-2025-08-21-123420

Tool Tab

3. Click List Tools

Shows all the MCP tools available from your server, like share_context, retrieve_context and list_all_context.

tools

Available Tools

4. We select the retrieve_context

share-context-tab

Share context tool

6. We can get all the stored context from all the agents by selecting the list_all_context

The source code can be downloaded from here.

We have build a simple and powerful MCP server in python which enables seamless context sharing among agents. Using the MCP protocol, we built tools to store data per agent and search it by query and tested everything with the MCP Inspector.