Langfuse observability integration for FastMCP v3 servers — one-line setup
Project description
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_idandclient_idfrom 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
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file fastmcp_langfuse-0.1.0.tar.gz.
File metadata
- Download URL: fastmcp_langfuse-0.1.0.tar.gz
- Upload date:
- Size: 5.5 kB
- Tags: Source
- Uploaded using 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}
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
8d88ed7c7e4d6bf3ad909f4cb89159889c91023b685667984239d1502c203b87
|
|
| MD5 |
727c0d158c056e058085988db9390b35
|
|
| BLAKE2b-256 |
1f017833ba649e1a87d595c9278eb6ae4561b235e99bf372f6700ec01716a8ca
|
File details
Details for the file fastmcp_langfuse-0.1.0-py3-none-any.whl.
File metadata
- Download URL: fastmcp_langfuse-0.1.0-py3-none-any.whl
- Upload date:
- Size: 7.6 kB
- Tags: Python 3
- Uploaded using 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}
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
c4cc2729e12f0c1f43a9b3b24d49ee92abcb6756caa57e3d1769becd5f878a20
|
|
| MD5 |
ebb65a489da6ca4d0fc0b91f5d42f9c1
|
|
| BLAKE2b-256 |
de95be4a679bd00d423201e95d49be2f8282a58a18ef6deaf88ec361fb4bf7e1
|