8000 Correct context for MCP logs by alexmojaki · Pull Request #1138 · pydantic/logfire · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

Correct context for MCP logs #1138

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. W 8000 e’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Jun 12, 2025
Merged

Correct context for MCP logs #1138

merged 3 commits into from
Jun 12, 2025

Conversation

alexmojaki
Copy link
Contributor
@alexmojaki alexmojaki commented Jun 11, 2025

Requires instrumenting both server and client. Example:

Screenshot 2025-06-11 at 16 11 18

Server:

from mcp.server.fastmcp import Context, FastMCP

import logfire

logfire.configure()
logfire.instrument_pydantic_ai()
logfire.instrument_mcp()

fastmcp = FastMCP()


@fastmcp.tool()
async def random_number(ctx: Context) -> int:  # type: ignore
    await ctx.info('Generating a random number')
    return 4


fastmcp.run('sse')

Client:

import asyncio

from pydantic_ai import Agent
from pydantic_ai.mcp import MCPServerHTTP

import logfire

logfire.configure()
logfire.instrument_pydantic_ai()
logfire.instrument_mcp()

server = MCPServerHTTP(url='http://localhost:8000/sse')
agent = Agent('openai:gpt-4o', mcp_servers=[server])


async def main():
    async with agent.run_mcp_servers():
        await agent.run('Generate a random number')


asyncio.run(main())

Copy link
cloudflare-workers-and-pages bot commented Jun 11, 2025

Deploying logfire-docs with  Cloudflare Pages  Cloudflare Pages

Latest commit: 31bdc3b
Status:⚡️  Build in progress...

View logs

Copy link
Contributor
@Copilot Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull Request Overview

This PR updates the OpenAI Agents MCP integration to correctly propagate OTEL context on both server- and client-side logging, and adjusts tests to reflect the new parent context values.

  • Refactors request/notification context attachment into helper functions and context managers
  • Adds notification instrumentation via send_notification
  • Updates tests to expect a non-null parent context for MCP server logs

Reviewed Changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated 2 comments.

File Description
tests/otel_integrations/test_openai_agents_mcp.py Updated expected context and added parent entries
logfire/_internal/integrations/mcp.py Extracted context-attachment logic, added notification instrumentation, and introduced context managers (_request_context, _handle_request_with_context)
Comments suppressed due to low confidence (1)

logfire/_internal/integrations/mcp.py:105

  • [nitpick] The _handle_request_with_context context manager is defined but not used in this diff. If it’s intended to replace inline logic in _handle_request, update that usage; otherwise, remove it to avoid dead code.
def _handle_request_with_context(request: Any, span_name: str):

@alexmojaki alexmojaki enabled auto-merge (squash) June 12, 2025 18:04
@alexmojaki alexmojaki merged commit b940118 into main Jun 12, 2025
13 of 14 checks passed
@alexmojaki alexmojaki deleted the alex/mcp-log-context branch June 12, 2025 18:08
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant
0