What is an MCP Server?
An MCP Server is a sophisticated, stateful server architecture engineered to manage and preserve modal or conversational contexts across multiple user interactions. It facilitates dynamic context switching and robust session state management, making it indispensable for applications requiring persistent user states, such as advanced chatbots, interactive gaming environments, and customer service platforms.
What Does 'Modal Context' Mean?
'Modal context' denotes distinct operational states or phases within an interactive session. For example, in a gaming scenario, a user might transition between 'playing mode' and 'waiting mode.' The MCP Server meticulously tracks and governs these states, ensuring precise responses and actions aligned with the active context.
What Does the MCP Server Do?
- Maintains a comprehensive history of user interactions to inform subsequent responses.
- Determines the optimal next action based on the prevailing context.
- Enables seamless transitions between diverse operational modes.
- Elevates the functionality and user experience of interactive applications.
How Does It Work?
The MCP Server functions as an intelligent context manager, continuously monitoring user inputs, updating the conversational state, and selecting appropriate responses or actions based on predefined protocols or advanced machine learning models. This ensures every interaction is processed within the correct modal framework, delivering a cohesive and responsive experience.
Example: Booking a Flight
Imagine a user initiating a flight booking process by stating, “I want to book a flight.” The MCP Server seamlessly shifts into a 'booking' context. When the user specifies, “From Lahore to Dubai,” the server interprets this within the established context. Should the user momentarily digress with, “What’s the weather in Dubai?” the server adeptly transitions to a 'weather inquiry' context, then efficiently returns to the booking workflow.

What Is Inside an MCP Server?
- A natural language processing (NLP) module to interpret and analyze user inputs.
- A state machine to meticulously track and manage conversational modes.
- A routing mechanism to direct requests to the appropriate handlers.
- A high-performance database or cache for storing session history and context data.
- An event manager to orchestrate asynchronous updates and notifications.
With the MCP SDK, these components are abstracted, providing a high-level interface for managing contexts, states, and events, while handling the underlying complexities.
Building an MCP Server with Python and MCP SDK
The Modal Context Protocol (MCP) SDK for Python offers a robust framework for creating stateful, context-aware servers. It simplifies the management of conversational contexts, states, and events, enabling developers to focus on application logic.
To begin, install the MCP SDK using pip:
pip install mcp-sdk
Below, we’ll walk through setting up an MCP Server, defining contexts, managing states, handling events, and integrating with FastAPI for real-time web interactions.
Initializing the MCP Server
First, initialize the MCP Server to manage all contexts and their states.
1
2
3
from mcp import MCPServer
server = MCPServer()
This creates an instance of the MCP Server, ready to handle context registration and processing.
Defining Contexts
Create custom contexts to manage specific conversational scenarios, such as a flight booking process.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
from mcp import Context, ContextState
class BookingContext(Context):
def __init__(self):
super().__init__()
self.state = ContextState.ACTIVE
self.data = {
'from_city': None,
'to_city': None,
'date': None
}
async def process_input(self, input_text: str) -> str:
if 'from' in input_text.lower():
self.data['from_city'] = input_text.split('from')[1].strip()
return f"Setting departure city to {self.data['from_city']}"
elif 'to' in input_text.lower():
self.data['to_city'] = input_text.split('to')[1].strip()
return f"Setting destination city to {self.data['to_city']}"
return 'Please specify from/to city'
server.register_context('booking', BookingContext)
This BookingContext
class processes user inputs to update booking details, maintaining state and data throughout the interaction.
Managing States and Transitions
For complex workflows, define multiple states and handle transitions between them.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
from mcp import StateTransition
class FlightBookingContext(Context):
STATES = ['INIT', 'COLLECTING_INFO', 'CONFIRMING', 'COMPLETED']
def __init__(self):
super().__init__()
self.state = 'INIT'
self.data = {}
async def transition_to(self, new_state: str):
if new_state in self.STATES:
old_state = self.state
self.state = new_state
await self.on_state_change(StateTransition(old_state, new_state))
async def on_state_change(self, transition: StateTransition):
print(f'Transitioning from {transition.from_state} to {transition.to_state}')
async def process_input(self, input_text: str) -> str:
if self.state == 'INIT':
await self.transition_to('COLLECTING_INFO')
return 'Starting new booking. Please specify departure city.'
if self.state == 'COLLECTING_INFO':
if 'from' in input_text.lower():
self.data['from_city'] = input_text.split('from')[1].strip()
return 'Great! Now tell me the destination city.'
elif 'to' in input_text.lower():
self.data['to_city'] = input_text.split('to')[1].strip()
if 'from_city' in self.data:
await self.transition_to('CONFIRMING')
return f"Confirm booking from {self.data['from_city']} to {self.data['to_city']}? (yes/no)"
return 'Please specify the departure city first.'
if self.state == 'CONFIRMING':
if input_text.lower() == 'yes':
await self.transition_to('COMPLETED')
return 'Booking confirmed!'
elif input_text.lower() == 'no':
await self.transition_to('INIT')
return 'Booking cancelled. Start over?'
return 'I did not understand that.'
This context guides users through a multi-step booking process, transitioning between states based on input.
Handling Events
Handle custom events, like price updates, to enable real-time responsiveness.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
from mcp import Event
class BookingEvent(Event):
def __init__(self, event_type: str, data: dict):
super().__init__(event_type)
self.data = data
class EventAwareContext(Context):
async def handle_event(self, event: BookingEvent):
if event.type == 'price_change':
return f"Alert: Price changed to {event.data['new_price']}"
return 'Event handled'
async def main():
context = EventAwareContext()
event = BookingEvent('price_change', {'new_price': '$299'})
response = await context.handle_event(event)
print(response) # Outputs: Alert: Price changed to $299
Events allow the server to react to external changes, enhancing interactivity.
Integrating with FastAPI
Expose the MCP Server over the web using FastAPI and WebSockets for real-time communication.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
from fastapi import FastAPI, WebSocket
from mcp import MCPServer
app = FastAPI()
server = MCPServer()
@app.websocket('/ws')
async def websocket_endpoint(websocket: WebSocket):
await websocket.accept()
context = await server.get_context('booking')
try:
while True:
text = await websocket.receive_text()
response = await context.process_input(text)
await websocket.send_text(response)
except Exception as e:
print(f'Error: {e}')
# Run with: uvicorn app:app --reload
This integration enables clients to interact with the MCP Server via a WebSocket endpoint, providing a seamless, real-time experience.
Why Use an MCP Server?
- Enhances user interactions by maintaining seamless context awareness.
- Ensures conversational continuity, minimizing repetitive inputs.
- Empowers applications with sophisticated, responsive behaviors.
- Streamlines development by abstracting complex state management processes.
How Is It Different?
Unlike traditional stateless servers, which process each request in isolation without retaining prior data, MCP Servers are inherently stateful. They preserve contextual information across sessions, a critical feature for applications demanding sustained user engagement and continuity.
Where Is It Used?
- Conversational AI and chatbots powering customer support and virtual assistance.
- Interactive gaming platforms to manage player states and progression.
- E-commerce and service platforms for streamlined, guided user experiences, such as booking or purchasing workflows.
Summary
MCP Servers are a cornerstone of intelligent, context-aware applications, leveraging conversational history to deliver highly relevant, efficient, and premium user experiences.
Technical Summary
The MCP Server is a stateful architecture designed to oversee modal and conversational contexts, supporting dynamic switching, session persistence, and asynchronous event handling for advanced interactive systems.
Further Reading
- Designing Contextual AI Agents
- State Machines and Context Switching in Software Design
- Building and Managing Stateful APIs
About the Author
Zeeshan Ali is a distinguished software developer specializing in intelligent applications and interactive systems. Explore his innovative projects on GitHub: Zeeshan Ali's GitHub.
