Skip to main content

The Pydantic Logfire MCP server! 🔍

Project description

Pydantic Logfire MCP Server

This repository contains a Model Context Protocol (MCP) server with tools that can access the OpenTelemetry traces and metrics you've sent to Pydantic Logfire.

Pydantic Logfire Server MCP server

This MCP server enables LLMs to retrieve your application's telemetry data, analyze distributed traces, and make use of the results of arbitrary SQL queries executed using the Pydantic Logfire APIs.

Available Tools

  • find_exceptions_in_file - Get the details about the 10 most recent exceptions on the file.

    • Arguments:
      • filepath (string) - The path to the file to find exceptions in.
      • age (integer) - Number of minutes to look back, e.g. 30 for last 30 minutes. Maximum allowed value is 7 days.
  • arbitrary_query - Run an arbitrary query on the Pydantic Logfire database.

    • Arguments:
      • query (string) - The query to run, as a SQL string.
      • age (integer) - Number of minutes to look back, e.g. 30 for last 30 minutes. Maximum allowed value is 7 days.
  • logfire_link - Creates a link to help the user to view the trace in the Logfire UI.

    • Arguments:
      • trace_id (string) - The trace ID to link to.
  • schema_reference - The database schema for the Logfire DataFusion database.

Setup

Install uv

The first thing to do is make sure uv is installed, as uv is used to run the MCP server.

For installation instructions, see the uv installation docs.

If you already have an older version of uv installed, you might need to update it with uv self update.

Obtain a Pydantic Logfire read token

In order to make requests to the Pydantic Logfire APIs, the Pydantic Logfire MCP server requires a "read token".

You can create one under the "Read Tokens" section of your project settings in Pydantic Logfire: https://logfire.pydantic.dev/-/redirect/latest-project/settings/read-tokens

[!IMPORTANT] Pydantic Logfire read tokens are project-specific, so you need to create one for the specific project you want to expose to the Pydantic Logfire MCP server.

Manually run the server

Once you have uv installed and have a Pydantic Logfire read token, you can manually run the MCP server using uvx (which is provided by uv).

You can specify your read token using the LOGFIRE_READ_TOKEN environment variable:

LOGFIRE_READ_TOKEN=YOUR_READ_TOKEN uvx logfire-mcp@latest

You can also set LOGFIRE_READ_TOKEN in a .env file:

LOGFIRE_READ_TOKEN=pylf_v1_us_...

NOTE: for this to work, the MCP server needs to run with the directory containing the .env file in its working directory.

or using the --read-token flag:

uvx logfire-mcp@latest --read-token=YOUR_READ_TOKEN

[!NOTE] If you are using Cursor, Claude Desktop, Cline, or other MCP clients that manage your MCP servers for you, you do NOT need to manually run the server yourself. The next section will show you how to configure these clients to make use of the Pydantic Logfire MCP server.

Base URL

If you are running Logfire in a self hosted environment, you need to specify the base URL. This can be done using the LOGFIRE_BASE_URL environment variable:

LOGFIRE_BASE_URL=https://logfire.my-company.com uvx logfire-mcp@latest --read-token=YOUR_READ_TOKEN

You can also use the --base-url argument:

uvx logfire-mcp@latest --base-url=https://logfire.my-company.com --read-token=YOUR_READ_TOKEN

Configuration with well-known MCP clients

Configure for Cursor

Create a .cursor/mcp.json file in your project root:

{
  "mcpServers": {
    "logfire": {
      "command": "uvx",
      "args": ["logfire-mcp@latest", "--read-token=YOUR-TOKEN"]
    }
  }
}

The Cursor doesn't accept the env field, so you need to use the --read-token flag instead.

Configure for Claude code

Run the following command:

claude mcp add logfire -e LOGFIRE_READ_TOKEN=YOUR_TOKEN -- uvx logfire-mcp@latest

Configure for Claude Desktop

Add to your Claude settings:

{
  "command": ["uvx"],
  "args": ["logfire-mcp@latest"],
  "type": "stdio",
  "env": {
    "LOGFIRE_READ_TOKEN": "YOUR_TOKEN"
  }
}

Configure for Cline

Add to your Cline settings in cline_mcp_settings.json:

{
  "mcpServers": {
    "logfire": {
      "command": "uvx",
      "args": ["logfire-mcp@latest"],
      "env": {
        "LOGFIRE_READ_TOKEN": "YOUR_TOKEN"
      },
      "disabled": false,
      "autoApprove": []
    }
  }
}

Configure for VS Code

Make sure you enabled MCP support in VS Code.

Create a .vscode/mcp.json file in your project's root directory:

{
  "servers": {
    "logfire": {
      "type": "stdio",
      "command": "uvx", // or the absolute /path/to/uvx
      "args": ["logfire-mcp@latest"],
      "env": {
        "LOGFIRE_READ_TOKEN": "YOUR_TOKEN"
      }
    }
  }
}

Configure for Zed

Create a .zed/settings.json file in your project's root directory:

{
  "context_servers": {
    "logfire": {
      "source": "custom",
      "command": "uvx",
      "args": ["logfire-mcp@latest"],
      "env": {
        "LOGFIRE_READ_TOKEN": "YOUR_TOKEN"
      },
      "enabled": true
    }
  }
}

Example Interactions

  1. Get details about exceptions from traces in a specific file:
{
  "name": "find_exceptions_in_file",
  "arguments": {
    "filepath": "app/api.py",
    "age": 1440
  }
}

Response:

[
  {
    "created_at": "2024-03-20T10:30:00Z",
    "message": "Failed to process request",
    "exception_type": "ValueError",
    "exception_message": "Invalid input format",
    "function_name": "process_request",
    "line_number": "42",
    "attributes": {
      "service.name": "api-service",
      "code.filepath": "app/api.py"
    },
    "trace_id": "1234567890abcdef"
  }
]
  1. Run a custom query on traces:
{
  "name": "arbitrary_query",
  "arguments": {
    "query": "SELECT trace_id, message, created_at, attributes->>'service.name' as service FROM records WHERE severity_text = 'ERROR' ORDER BY created_at DESC LIMIT 10",
    "age": 1440
  }
}

Examples of Questions for Claude

  1. "What exceptions occurred in traces from the last hour across all services?"
  2. "Show me the recent errors in the file 'app/api.py' with their trace context"
  3. "How many errors were there in the last 24 hours per service?"
  4. "What are the most common exception types in my traces, grouped by service name?"
  5. "Get me the OpenTelemetry schema for traces and metrics"
  6. "Find all errors from yesterday and show their trace contexts"

Getting Started

  1. First, obtain a Pydantic Logfire read token from: https://logfire.pydantic.dev/-/redirect/latest-project/settings/read-tokens

  2. Run the MCP server:

    uvx logfire-mcp@latest --read-token=YOUR_TOKEN
    
  3. Configure your preferred client (Cursor, Claude Desktop, or Cline) using the configuration examples above

  4. Start using the MCP server to analyze your OpenTelemetry traces and metrics!

Contributing

We welcome contributions to help improve the Pydantic Logfire MCP server. Whether you want to add new trace analysis tools, enhance metrics querying functionality, or improve documentation, your input is valuable.

For examples of other MCP servers and implementation patterns, see the Model Context Protocol servers repository.

License

Pydantic Logfire MCP is licensed under the MIT License. This means you are free to use, modify, and distribute the software, subject to the terms and conditions of the MIT License.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

iflow_mcp_logfire_mcp-0.6.2.tar.gz (88.1 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

iflow_mcp_logfire_mcp-0.6.2-py3-none-any.whl (9.3 kB view details)

Uploaded Python 3

File details

Details for the file iflow_mcp_logfire_mcp-0.6.2.tar.gz.

File metadata

File hashes

Hashes for iflow_mcp_logfire_mcp-0.6.2.tar.gz
Algorithm Hash digest
SHA256 ceda13e89a096cb5c3cbb4f6600d351392cf7e6d36cd4d1d596415ee2e1cc472
MD5 a76bb9b0b31f56d75e4d8ee4945dbde1
BLAKE2b-256 f7f4aca21cfbf233d3caa69efd83d35a8d290a906127a7dd32e3a2ebf9eca608

See more details on using hashes here.

File details

Details for the file iflow_mcp_logfire_mcp-0.6.2-py3-none-any.whl.

File metadata

File hashes

Hashes for iflow_mcp_logfire_mcp-0.6.2-py3-none-any.whl
Algorithm Hash digest
SHA256 f60f4ee4e18e6e2e0240189f4a865db1cb0d0ddd06f7076bd10391dbf7f21757
MD5 ce1f6a83fa1a81381516d0906af11468
BLAKE2b-256 39b679c9017a5c463e57b5e442fe42524312de85f9d5aeaa03d28ec14513e035

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page