Sponsored by Deepsite.site

Weave MCP Server + Client Linked Traces:

Created By
zbirenbaum8 months ago
Content

Weave MCP Server + Client Linked Traces:

This repo is taken from the example in Arize-ai/phoenix and adapted to export to wandb/weave. Note: There is a race condition which sometimes causes the tool to fail to run at the OpenAI call step. This bug was also present in the original and was not introduced by weave.

Set up your environment:

First run cp .env.example .env Follow the instructions and set the relevant keys in your new env file.

Install Dependencies:

  • uv: uv sync
  • pip: pip install -r requirements.txt

Run the client and export traces

  • uv: uv run client.py
  • python: python client.py

(From Arize) How to Implement End-to-End Tracing for MCP Client-Server Applications

This tutorial shows you how to propagate OpenTelemetry (OTEL) context between an MCP client and server for complete observability. The openinference-instrumentation-mcp package makes this possible by providing instrumentation for both client and server components.

What is MCP and Why Do You Need Distributed Tracing?

One of the main benefits of Anthropic's Model Context Protocol (MCP) architecture is connecting AI models with information across different services, machines, and programming languages. This distributed approach delivers several advantages:

  1. Expanded AI Capabilities: Connect models to specialized knowledge and data sources beyond their training data
  2. Plug-and-Play Components: Add new context providers without retraining your models
  3. Multi-Language Support: Implement context providers in any programming language while maintaining compatibility

The challenge? When requests flow through multiple services, debugging becomes difficult. How do you track a request's complete journey to identify where problems occur?

How to Use OpenTelemetry for MCP Tracing

OpenTelemetry solves cross-service tracing challenges by:

  • Preserving Context Across Services: Maintaining trace IDs and relationships between different components
  • Working Across Network Boundaries: Automatically handling context in network requests
  • Supporting Multiple Languages: Using standardized formats compatible with any programming language

When your client calls the server with proper instrumentation:

  1. The client creates a span to track the operation
  2. OTEL context automatically travels with the MCP request
  3. The server continues the same trace without interruption
  4. All context providers inherit this trace context
  5. You see the complete interaction as one connected trace in Phoenix

This visibility is essential for troubleshooting complex AI systems, optimizing performance bottlenecks, and understanding how different components affect your application's behavior.

Setup

When properly instrumented, trace context is automatically propagated across the MCP client-server boundary, allowing you to:

  • Track requests from client to server in a single trace
  • Observe latency at different stages of the request lifecycle
  • Debug issues that span across service boundaries

Env Setup

  1. Navigate to this directory:

    cd tutorials/mcp/tracing_between_mcp_client_and_server
    
  2. Install the required dependencies:

    pip install -r requirements.txt
    

Running the Example

  1. Run Phoenix locally, or connect to an instance online

  2. Update your .env file with OPENAI_API_KEY, and your PHOENIX_COLLECTOR_ENDPOINT. If you're using an online Phoenix instance or have auth enabled, also set your PHOENIX_API_KEY.

  3. Run the MCP client. The client code will spin up the server at run time in a separate process.

    python client.py
    
  4. Ask questions of the agent.

  5. View the traces in Phoenix: mcp-traces

How It Works

The openinference-instrumentation-mcp package automatically:

  1. Creates spans for MCP client operations
  2. Injects trace context into MCP requests
  3. Extracts and continues the trace context on the server side
  4. Associates the context with any OTEL spans created on the server side

This allows you to see the complete request flow as a single trace, even though it crosses service boundaries.

Recommend Servers
TraeBuild with Free GPT-4.1 & Claude 3.7. Fully MCP-Ready.
Jina AI MCP ToolsA Model Context Protocol (MCP) server that integrates with Jina AI Search Foundation APIs.
Amap Maps高德地图官方 MCP Server
BlenderBlenderMCP connects Blender to Claude AI through the Model Context Protocol (MCP), allowing Claude to directly interact with and control Blender. This integration enables prompt assisted 3D modeling, scene creation, and manipulation.
Playwright McpPlaywright MCP server
AiimagemultistyleA Model Context Protocol (MCP) server for image generation and manipulation using fal.ai's Stable Diffusion model.
Zhipu Web SearchZhipu Web Search MCP Server is a search engine specifically designed for large models. It integrates four search engines, allowing users to flexibly compare and switch between them. Building upon the web crawling and ranking capabilities of traditional search engines, it enhances intent recognition capabilities, returning results more suitable for large model processing (such as webpage titles, URLs, summaries, site names, site icons, etc.). This helps AI applications achieve "dynamic knowledge acquisition" and "precise scenario adaptation" capabilities.
ChatWiseThe second fastest AI chatbot™
Baidu Map百度地图核心API现已全面兼容MCP协议,是国内首家兼容MCP协议的地图服务商。
CursorThe AI Code Editor
MCP AdvisorMCP Advisor & Installation - Use the right MCP server for your needs
TimeA Model Context Protocol server that provides time and timezone conversion capabilities. This server enables LLMs to get current time information and perform timezone conversions using IANA timezone names, with automatic system timezone detection.
Tavily Mcp
Visual Studio Code - Open Source ("Code - OSS")Visual Studio Code
WindsurfThe new purpose-built IDE to harness magic
Howtocook Mcp基于Anduin2017 / HowToCook (程序员在家做饭指南)的mcp server,帮你推荐菜谱、规划膳食,解决“今天吃什么“的世纪难题; Based on Anduin2017/HowToCook (Programmer's Guide to Cooking at Home), MCP Server helps you recommend recipes, plan meals, and solve the century old problem of "what to eat today"
EdgeOne Pages MCPAn MCP service designed for deploying HTML content to EdgeOne Pages and obtaining an accessible public URL.
MiniMax MCPOfficial MiniMax Model Context Protocol (MCP) server that enables interaction with powerful Text to Speech, image generation and video generation APIs.
Context7Context7 MCP Server -- Up-to-date code documentation for LLMs and AI code editors
Serper MCP ServerA Serper MCP Server
DeepChatYour AI Partner on Desktop