Skip to main content

fastmcp-langfuse

Plug Langfuse observability into any FastMCP v3 server. One function call, zero boilerplate.

from langfuse import get_client
get_client()

from fastmcp import FastMCP
from fastmcp_langfuse import instrument

mcp = FastMCP("My Server")
instrument(mcp)

@mcp.tool()
def anything(x: str) -> str:   # fully traced, just like that
    return "hello"

Three imports, one instrument() call. Vibecode the rest -- every tool, resource, and prompt gets session tracking, I/O capture, environment tags, and trace URLs automatically.

Install

pip install fastmcp-langfuse
# or
uv add fastmcp-langfuse

You'll also need langfuse in your project -- this library sets the right OTel span attributes that the Langfuse SDK picks up, but doesn't import it directly.

What you get for free

Once you call instrument(mcp), every handler registered after it gets:

  • Input/output -- function args serialised as input, return value as output
  • Session & user tracking -- session_id and client_id from FastMCP context
  • Environment & release tags -- tag your traces with production, staging, etc.
  • Trace URLs -- clickable Langfuse links printed to your console

No decorators on every handler. No manual span enrichment. Just works.

Configure it

Pass kwargs directly or use a config object -- whatever feels right:

# kwargs
instrument(mcp, environment="production", release="v1.2.3", trace_urls=False)

# or config object
from fastmcp_langfuse import LangfuseConfig

instrument(mcp, config=LangfuseConfig(environment="production"))
Field Type Default What it does
environment str "" Tags langfuse.environment on every span
release str "" Tags langfuse.release on every span
session_tracking bool True Sets session/user IDs from FastMCP context
trace_urls bool True Prints Langfuse trace URLs to console
base_url str https://cloud.langfuse.com Your Langfuse instance URL

Need more control?

Use @langfuse_trace on individual handlers instead of (or alongside) instrument():

from fastmcp_langfuse import langfuse_trace

@mcp.tool()
@langfuse_trace
def special_tool(x: str) -> str:
    return "traced with the decorator"

Same enrichment, just explicit.

How it works under the hood

FastMCP auto-creates OTel spans for every tool/resource/prompt call. This library wraps your handlers to enrich those spans with Langfuse-specific attributes (langfuse.session.id, langfuse.trace.input, etc.) that the Langfuse SDK recognises when exporting.

The library only depends on opentelemetry-api and fastmcp. Lightweight, no opinions about your config management.

Examples

Check out examples/ for a full working demo with tools, resources, prompts, cost tracking, user feedback, and distributed tracing. It uses pydantic-settings to bridge env vars to LangfuseConfig -- but that's just one way to do it, bring your own config however you want.

cd examples && cp .env.example .env
# add your LANGFUSE_PUBLIC_KEY and LANGFUSE_SECRET_KEY

uv run python examples/server.py   # terminal 1
uv run python examples/client.py   # terminal 2

License

MIT

Metadata

Release files for fastmcp-langfuse 0.1.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for fastmcp-langfuse 0.1.0
File Size Uploaded Provenance
fastmcp_langfuse-0.1.0.tar.gz 5.5 kB Without provenance Details

Built distribution (wheel)

Table of built distributions (wheels) for fastmcp-langfuse 0.1.0
File Interpreter ABI Platform
fastmcp_langfuse-0.1.0-py3-none-any.whl Python 3 none any Details

Total release size: 13.1 kB

Release files / fastmcp_langfuse-0.1.0.tar.gz

Download URL fastmcp_langfuse-0.1.0.tar.gz
Size 5.5 kB
Tags Source
SHA-256 checksum
How to use checksums
8d88ed7c7e4d6bf3ad909f4cb89159889c91023b685667984239d1502c203b87
BLAKE2b-256 checksum
How to use checksums
1f017833ba649e1a87d595c9278eb6ae4561b235e99bf372f6700ec01716a8ca
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via uv/0.10.2 {"installer":{"name":"uv","version":"0.10.2","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"24.04","id":"noble","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}

Release files / fastmcp_langfuse-0.1.0-py3-none-any.whl

Download URL fastmcp_langfuse-0.1.0-py3-none-any.whl
Size 7.6 kB
Tags Python 3
SHA-256 checksum
How to use checksums
c4cc2729e12f0c1f43a9b3b24d49ee92abcb6756caa57e3d1769becd5f878a20
BLAKE2b-256 checksum
How to use checksums
de95be4a679bd00d423201e95d49be2f8282a58a18ef6deaf88ec361fb4bf7e1
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via uv/0.10.2 {"installer":{"name":"uv","version":"0.10.2","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"24.04","id":"noble","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}

Security for fastmcp-langfuse 0.1.0

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

Release history Release notifications | RSS feed

This release

0.1.0 This release

2 release files No provenance

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page