How to Setup Observability for your MCP Server with Moesif
The Model Context Protocol (MCP) has taken the internet by storm by rapidly becoming the standard for Large Language Models (LLMs) to communicate with external data sources or tools. MCP provides a structured way to fetch data and trigger workflows through APIs and functions. However, with great power comes great responsibility. MCP exposes a tremendous amount of data and capabilities via high-volume interactions driven by AI agents causing unique challenges for monitoring and maintaining the reliability of your MCP server. This is where observability comes into play. Moesif API observability is uniquely designed to provide deep visibility into your MCP traffic and resolve experience issues before they become a greater concern or security threat. You can also gain business level analytics to understand how prompts and agents are interacting with your MCP server.
Why is Observability Important for MCP?
Observability for MCP provides performance and usage statistics on how LLMs are interacting with your tools and data. This can be both users entering prompts into a conversational interface, but also AI agents interacting with your tools programmatically. Unlike a traditional user interface where the “customer journeys” are predefined, MCP servers provide raw access to your tools which can be called and fetched in unexpected ways. In addition, the machine driven nature of AI agents can cause far larger variances in usage pattern and customer experience metrics. Thus, it’s critical to measure “AI Experience” ensuring your tools are reliable and useful.
As a new technology still early in development, MCP server implementations are being developed and shipped at record pace. This increases the probability of defects and issues dramatically in production. Observability provides you peace of mind to ship fast knowing you have good metrics in place to know if a change is impacting customers. This can help you proactively identify and diagnose issues before they impact users.
Furthermore, the open nature exposes your application to a new frontier of potential security vulnerabilities. Bad actors could potentially scrape massive amounts of proprietary or sensitive data. Unintentional or intentional abuse can impact reliability and performance of other legitimate users of your MCP server. Similarly, malicious actors can cause economic harm through excessive or abusive queries. Strong observability enables you to identify bad actors and ensure the right guardrails are in place.
Why is Observability Hard for MCP?
Achieving effective observability for MCP servers presents several hurdles. Unlike traditional applications with relatively static API endpoints, MCP interactions are characterized by highly dynamic prompts and queries generated by LLMs. This makes it difficult to rely on predefined metrics, alert rules, or dashboards. Traditional observability tooling doesn’t capture enough context to be useful when troubleshooting problems around your MCP server and usage.
In addition, the sheer volume of requests can also be significantly higher, especially when AI agents are continuously interacting with the server, dwarfing the traffic seen by interactive websites. Real User Monitoring (RUM) metrics designed for frontend applications can’t be leveraged for machine generated data. This requires an observability solution that’s uniquely designed for tracking deep context, but also high volume of data.
Lastly, MCP in production relies on server-side events and the new HTTP Streamable HTTP support. Traditional observability tooling was not designed for these newer asynchronous and real-time protocols.
How Moesif solves observability for MCP?
Moesif is an API observability and analytics solution uniquely tailored for MCP servers. A key advantage of Moesif is its ability to provide deep visibility into JSON-RPC payloads, the standard communication format for MCP. This means you can not only see the raw requests but also understand the specific data being fetched or the actions being triggered by the LLM. This level of detail is crucial for understanding the context of each interaction and diagnosing issues effectively.
Beyond debugging and performance monitoring, Moesif also offers the capability to understand and monetize usage of your MCP server. Your organization is likely sitting on a treasure trove of data which can suddenly be monetized via MCP. By tracking requests and usage patterns, you can implement metering and billing based on consumption and outcomes.
Integrating Moesif with your MCP server is straightforward. There is out-of-the-box support for most frameworks used to build MCP servers including FastAPI, Node.js, and Spring Boot. In addition, plugins available for most API gateways like WSO2, Kong, Amazon API Gateway, and others.
Example MCP Server with Moesif API Observability
As part of this tutorial, we’ll walk through setting up Moesif API Observability for an example MCP server running on Python and Starlette.
A working example is available on GitHub here. This is a modified version of Anthropic’s reference MCP servers.
Prerequisites:
- Python and uv are installed.
- Your MCP server is set up to use Server-Sent Events (SSE) or the new Streamable HTTP.
1. Install Moesif
First, install the moesifasgi
package which is compatible with FastAPI, Starlette, and other ASGI-based frameworks:
uv add moesifasgi
2. Enable the middleware
If you don’t already have it, you can get your Moesif Application Id by signing up for a free account.
from moesifasgi import MoesifMiddleware
moesif_settings = {
'APPLICATION_ID': 'YOUR_MOESIF_APPLICATION_ID'
}
# Add Moesif to your starlette app
starlette_app.add_middleware(MoesifMiddleware, settings=moesif_settings)
# Run the app
uvicorn.run(starlette_app, host="0.0.0.0", port=3001, log_level="info")
Remember to replace “YOUR_MOESIF_APPLICATION_ID” with your actual Moesif application ID obtained from your Moesif dashboard.
3. Run the MCP server
You can run the MCP server with the following command:
uv run src/mcp_server_fetch
It is also crucial to implement robust security measures for production to prevent unauthorized access and vulnerabilities. This includes best practices for input validation, access control, and error handling to safeguard against malicious attacks.
4. Run the MCP Client Tool (Optional)
To trigger functions without going through the Anthropic Claude app, you can run the local client.
npx @modelcontextprotocol/inspector
Viewing MCP Traffic
Once Moesif is integrated, log in to your Moesif dashboard and navigate to the live event log.
You should see your MCP traffic flowing in. From there you can inspect the detailed JSON-RPC request. Moesif provides a rich interface to explore your API calls, filter by various criteria (e.g., method, status code, user ID), and inspect the detailed JSON-RPC request and response payloads. You can create custom dashboards, set up alerts for specific events or thresholds, and gain invaluable insights into the behavior and performance of your MCP server.
Optimizing performance is essential for managing and troubleshooting MCP traffic effectively. You can create custom dashboards, set up alerts for specific events or thresholds, and gain invaluable insights into the behavior and performance of your MCP server.
Identifying Users
So far we setup monitoring of your MCP server’s traffic. To better understand usage patterns across different users and AI agents, it’s recommended to also identify the user of the request. This can be done using the middleware options. You can extract the user id from an Authorization token or other context variable.
def identify_user(request, response):
# Your custom code that returns a user id string
'12345'
MOESIF_MIDDLEWARE = {
'APPLICATION_ID': 'YOUR_MOESIF_APPLICATION_ID',
'IDENTIFY_USER': identify_user,
}
Conclusion
As the Model Context Protocol continues to evolve and drive new AI use cases, the need for robust observability becomes paramount. Moesif offers a purpose-built solution to tackle the unique challenges of monitoring MCP servers, providing deep visibility into dynamic payloads, high-volume traffic, and emerging communication patterns. By integrating Moesif, developers and operators can proactively ensure the reliability, security, and optimal performance of their MCP deployments, ultimately fostering better “AI Experiences” and unlocking the full potential of this transformative technology.