Emit MCP operation and session duration metrics (OTel semantic conventions)

オープン
#3,945 コメント 0 件 リアクション 0 件 担当者 0 名 GitHub で見る

メンテナーはふだん 1 日以内に返信

まだ誰も着手していません。

評価

難易度
4/5
見積もり時間
3〜5日
初心者へのやさしさ
56/100
issue の種類
機能追加
明瞭さ
おおむね明確
活発さ
活発
技術スタック
python
領域
observability

調査の方向性

Start with src/mcp/server/_otel.py and src/mcp/shared/_otel.py to follow the existing tracing setup, then inspect the dispatcher and session start/end paths mentioned in the proposal. Compare metric names and attributes with the referenced semantic conventions and scenario. Done means client and server operation and session durations are recorded with the specified attributes, while remaining no-op without a configured MeterProvider.

索引モデルが issue の本文から書いたものです。

説明

Description

The SDK already creates OpenTelemetry spans by default (OpenTelemetryMiddleware on the server, a CLIENT span per outbound request in the dispatcher), but it doesn't record any metrics. There's no meter in src/mcp today.

The OTel GenAI semantic conventions define four MCP histograms (all in development status):

  • mcp.client.operation.duration (s)
  • mcp.server.operation.duration (s)
  • mcp.client.session.duration (s)
  • mcp.server.session.duration (s)

Right now these only show up through the reference scenario in the semconv repo (reference/scenarios/mcp/scenario.py), which builds the histograms itself and patches serve_connection. Users running the SDK don't get them.

Use case: I run MCP servers in production and send telemetry to Splunk through the OTel Collector. Spans are useful for debugging one request, but for dashboards and alerts (p95 tool-call latency, error rate per method, session length) I need metrics. Deriving them from spans is expensive and breaks when traces are sampled.

Proposal:

  • Use opentelemetry-api's get_meter("mcp-python-sdk"), same pattern as the existing tracer, so it's a no-op unless the app configures a MeterProvider.
  • Record server operation duration in OpenTelemetryMiddleware, reusing the attributes it already sets (mcp.method.name, gen_ai.tool.name, error.type, etc.).
  • Record client operation duration around the existing span in the dispatcher.
  • Record session durations at session start/end, with the negotiated protocol version.
  • No new dependency and no behavior change without a configured MeterProvider.

Questions before I start:

  1. Is this something you'd accept?
  2. Should the client-side metric wait for the outbound middleware mentioned in the TODO in jsonrpc_dispatcher.py, or is it fine to add next to the current span?
  3. Since the conventions are still in development, do you want this behind an opt-in flag?

Happy to send a PR if the direction looks right.

References
主要言語
Python
スター
25k
フォーク
4.2k
平均マージ
10分
マージ済み PR(30日)
1

環境構築

はじめの一歩

  1. issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
  2. 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
  3. リポジトリをフォークし、ブランチを切って変更します。
  4. issue 番号を参照したプルリクエストを送ります。

modelcontextprotocol/python-sdk のほかの issue

modelcontextprotocol/python-sdk の issue をすべて見る

似ている issue

Python の issue をもっと見る

新しい issue をメールで受け取る

初心者向けの GitHub issue を短くまとめたダイジェスト。