Skip to content

Commit 10f1f8f

Browse files
committed
Add OpenTelemetry instrumentation for Model Context Protocol (MCP)
This commit introduces a new instrumentation package for the Model Context Protocol (MCP), enabling automatic distributed tracing for MCP client-server interactions. Key Features: - Automatic instrumentation of MCP client sessions and server handlers - Support for both stdio and HTTP/SSE transports - W3C trace context propagation across MCP messages - Comprehensive span attributes following semantic conventions - Tool calls, resource reads, prompt handling, and session lifecycle tracing Package Structure: - Core instrumentation: McpInstrumentor with automatic wrapping - Semantic attributes: MCP-specific span attributes and conventions - Test coverage: Unit tests for instrumentor, server, and session wrappers - Examples: stdio (simple-client-server) and HTTP/SSE transport demos - Documentation: README, CHANGELOG, and example-specific guides Technical Implementation: - Wraps mcp.ClientSession and mcp.server.Server classes - Injects trace context into message metadata - Extracts and propagates context across transport boundaries - Records exceptions and sets span status appropriately - Requires Python >=3.10 (MCP SDK requirement) CI/CD Updates: - Added MCP instrumentation to test workflows - Updated lint configuration for new package - Disabled pypy3 tests (Python 3.10+ requirement) - Integrated into eachdist.ini and bootstrap generation Examples: - simple-client-server: stdio transport with tool calls and resources - http-transport: HTTP/SSE transport with distributed tracing - Both include Jaeger trace visualizations and setup instructions"
1 parent ebe5bdf commit 10f1f8f

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

46 files changed

+7117
-1014
lines changed

.github/workflows/lint_0.yml

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,25 @@ jobs:
8989
- name: Run tests
9090
run: tox -e lint-instrumentation-google-genai
9191

92+
lint-instrumentation-mcp:
93+
name: instrumentation-mcp
94+
runs-on: ubuntu-latest
95+
timeout-minutes: 30
96+
steps:
97+
- name: Checkout repo @ SHA - ${{ github.sha }}
98+
uses: actions/checkout@v4
99+
100+
- name: Set up Python 3.13
101+
uses: actions/setup-python@v5
102+
with:
103+
python-version: "3.13"
104+
105+
- name: Install tox
106+
run: pip install tox-uv
107+
108+
- name: Run tests
109+
run: tox -e lint-instrumentation-mcp
110+
92111
lint-resource-detector-containerid:
93112
name: resource-detector-containerid
94113
runs-on: ubuntu-latest

.github/workflows/test_0.yml

Lines changed: 152 additions & 152 deletions
Original file line numberDiff line numberDiff line change
@@ -640,6 +640,158 @@ jobs:
640640
- name: Run tests
641641
run: tox -e py313-test-instrumentation-google-genai-latest -- -ra
642642

643+
py310-test-instrumentation-mcp-oldest_ubuntu-latest:
644+
name: instrumentation-mcp-oldest 3.10 Ubuntu
645+
runs-on: ubuntu-latest
646+
timeout-minutes: 30
647+
steps:
648+
- name: Checkout repo @ SHA - ${{ github.sha }}
649+
uses: actions/checkout@v4
650+
651+
- name: Set up Python 3.10
652+
uses: actions/setup-python@v5
653+
with:
654+
python-version: "3.10"
655+
656+
- name: Install tox
657+
run: pip install tox-uv
658+
659+
- name: Run tests
660+
run: tox -e py310-test-instrumentation-mcp-oldest -- -ra
661+
662+
py310-test-instrumentation-mcp-latest_ubuntu-latest:
663+
name: instrumentation-mcp-latest 3.10 Ubuntu
664+
runs-on: ubuntu-latest
665+
timeout-minutes: 30
666+
steps:
667+
- name: Checkout repo @ SHA - ${{ github.sha }}
668+
uses: actions/checkout@v4
669+
670+
- name: Set up Python 3.10
671+
uses: actions/setup-python@v5
672+
with:
673+
python-version: "3.10"
674+
675+
- name: Install tox
676+
run: pip install tox-uv
677+
678+
- name: Run tests
679+
run: tox -e py310-test-instrumentation-mcp-latest -- -ra
680+
681+
py311-test-instrumentation-mcp-oldest_ubuntu-latest:
682+
name: instrumentation-mcp-oldest 3.11 Ubuntu
683+
runs-on: ubuntu-latest
684+
timeout-minutes: 30
685+
steps:
686+
- name: Checkout repo @ SHA - ${{ github.sha }}
687+
uses: actions/checkout@v4
688+
689+
- name: Set up Python 3.11
690+
uses: actions/setup-python@v5
691+
with:
692+
python-version: "3.11"
693+
694+
- name: Install tox
695+
run: pip install tox-uv
696+
697+
- name: Run tests
698+
run: tox -e py311-test-instrumentation-mcp-oldest -- -ra
699+
700+
py311-test-instrumentation-mcp-latest_ubuntu-latest:
701+
name: instrumentation-mcp-latest 3.11 Ubuntu
702+
runs-on: ubuntu-latest
703+
timeout-minutes: 30
704+
steps:
705+
- name: Checkout repo @ SHA - ${{ github.sha }}
706+
uses: actions/checkout@v4
707+
708+
- name: Set up Python 3.11
709+
uses: actions/setup-python@v5
710+
with:
711+
python-version: "3.11"
712+
713+
- name: Install tox
714+
run: pip install tox-uv
715+
716+
- name: Run tests
717+
run: tox -e py311-test-instrumentation-mcp-latest -- -ra
718+
719+
py312-test-instrumentation-mcp-oldest_ubuntu-latest:
720+
name: instrumentation-mcp-oldest 3.12 Ubuntu
721+
runs-on: ubuntu-latest
722+
timeout-minutes: 30
723+
steps:
724+
- name: Checkout repo @ SHA - ${{ github.sha }}
725+
uses: actions/checkout@v4
726+
727+
- name: Set up Python 3.12
728+
uses: actions/setup-python@v5
729+
with:
730+
python-version: "3.12"
731+
732+
- name: Install tox
733+
run: pip install tox-uv
734+
735+
- name: Run tests
736+
run: tox -e py312-test-instrumentation-mcp-oldest -- -ra
737+
738+
py312-test-instrumentation-mcp-latest_ubuntu-latest:
739+
name: instrumentation-mcp-latest 3.12 Ubuntu
740+
runs-on: ubuntu-latest
741+
timeout-minutes: 30
742+
steps:
743+
- name: Checkout repo @ SHA - ${{ github.sha }}
744+
uses: actions/checkout@v4
745+
746+
- name: Set up Python 3.12
747+
uses: actions/setup-python@v5
748+
with:
749+
python-version: "3.12"
750+
751+
- name: Install tox
752+
run: pip install tox-uv
753+
754+
- name: Run tests
755+
run: tox -e py312-test-instrumentation-mcp-latest -- -ra
756+
757+
py313-test-instrumentation-mcp-oldest_ubuntu-latest:
758+
name: instrumentation-mcp-oldest 3.13 Ubuntu
759+
runs-on: ubuntu-latest
760+
timeout-minutes: 30
761+
steps:
762+
- name: Checkout repo @ SHA - ${{ github.sha }}
763+
uses: actions/checkout@v4
764+
765+
- name: Set up Python 3.13
766+
uses: actions/setup-python@v5
767+
with:
768+
python-version: "3.13"
769+
770+
- name: Install tox
771+
run: pip install tox-uv
772+
773+
- name: Run tests
774+
run: tox -e py313-test-instrumentation-mcp-oldest -- -ra
775+
776+
py313-test-instrumentation-mcp-latest_ubuntu-latest:
777+
name: instrumentation-mcp-latest 3.13 Ubuntu
778+
runs-on: ubuntu-latest
779+
timeout-minutes: 30
780+
steps:
781+
- name: Checkout repo @ SHA - ${{ github.sha }}
782+
uses: actions/checkout@v4
783+
784+
- name: Set up Python 3.13
785+
uses: actions/setup-python@v5
786+
with:
787+
python-version: "3.13"
788+
789+
- name: Install tox
790+
run: pip install tox-uv
791+
792+
- name: Run tests
793+
run: tox -e py313-test-instrumentation-mcp-latest -- -ra
794+
643795
py39-test-resource-detector-containerid_ubuntu-latest:
644796
name: resource-detector-containerid 3.9 Ubuntu
645797
runs-on: ubuntu-latest
@@ -4629,155 +4781,3 @@ jobs:
46294781

46304782
- name: Run tests
46314783
run: tox -e py313-test-instrumentation-starlette-latest -- -ra
4632-
4633-
pypy3-test-instrumentation-starlette-oldest_ubuntu-latest:
4634-
name: instrumentation-starlette-oldest pypy-3.9 Ubuntu
4635-
runs-on: ubuntu-latest
4636-
timeout-minutes: 30
4637-
steps:
4638-
- name: Checkout repo @ SHA - ${{ github.sha }}
4639-
uses: actions/checkout@v4
4640-
4641-
- name: Set up Python pypy-3.9
4642-
uses: actions/setup-python@v5
4643-
with:
4644-
python-version: "pypy-3.9"
4645-
4646-
- name: Install tox
4647-
run: pip install tox-uv
4648-
4649-
- name: Run tests
4650-
run: tox -e pypy3-test-instrumentation-starlette-oldest -- -ra
4651-
4652-
pypy3-test-instrumentation-starlette-latest_ubuntu-latest:
4653-
name: instrumentation-starlette-latest pypy-3.9 Ubuntu
4654-
runs-on: ubuntu-latest
4655-
timeout-minutes: 30
4656-
steps:
4657-
- name: Checkout repo @ SHA - ${{ github.sha }}
4658-
uses: actions/checkout@v4
4659-
4660-
- name: Set up Python pypy-3.9
4661-
uses: actions/setup-python@v5
4662-
with:
4663-
python-version: "pypy-3.9"
4664-
4665-
- name: Install tox
4666-
run: pip install tox-uv
4667-
4668-
- name: Run tests
4669-
run: tox -e pypy3-test-instrumentation-starlette-latest -- -ra
4670-
4671-
py39-test-instrumentation-jinja2_ubuntu-latest:
4672-
name: instrumentation-jinja2 3.9 Ubuntu
4673-
runs-on: ubuntu-latest
4674-
timeout-minutes: 30
4675-
steps:
4676-
- name: Checkout repo @ SHA - ${{ github.sha }}
4677-
uses: actions/checkout@v4
4678-
4679-
- name: Set up Python 3.9
4680-
uses: actions/setup-python@v5
4681-
with:
4682-
python-version: "3.9"
4683-
4684-
- name: Install tox
4685-
run: pip install tox-uv
4686-
4687-
- name: Run tests
4688-
run: tox -e py39-test-instrumentation-jinja2 -- -ra
4689-
4690-
py310-test-instrumentation-jinja2_ubuntu-latest:
4691-
name: instrumentation-jinja2 3.10 Ubuntu
4692-
runs-on: ubuntu-latest
4693-
timeout-minutes: 30
4694-
steps:
4695-
- name: Checkout repo @ SHA - ${{ github.sha }}
4696-
uses: actions/checkout@v4
4697-
4698-
- name: Set up Python 3.10
4699-
uses: actions/setup-python@v5
4700-
with:
4701-
python-version: "3.10"
4702-
4703-
- name: Install tox
4704-
run: pip install tox-uv
4705-
4706-
- name: Run tests
4707-
run: tox -e py310-test-instrumentation-jinja2 -- -ra
4708-
4709-
py311-test-instrumentation-jinja2_ubuntu-latest:
4710-
name: instrumentation-jinja2 3.11 Ubuntu
4711-
runs-on: ubuntu-latest
4712-
timeout-minutes: 30
4713-
steps:
4714-
- name: Checkout repo @ SHA - ${{ github.sha }}
4715-
uses: actions/checkout@v4
4716-
4717-
- name: Set up Python 3.11
4718-
uses: actions/setup-python@v5
4719-
with:
4720-
python-version: "3.11"
4721-
4722-
- name: Install tox
4723-
run: pip install tox-uv
4724-
4725-
- name: Run tests
4726-
run: tox -e py311-test-instrumentation-jinja2 -- -ra
4727-
4728-
py312-test-instrumentation-jinja2_ubuntu-latest:
4729-
name: instrumentation-jinja2 3.12 Ubuntu
4730-
runs-on: ubuntu-latest
4731-
timeout-minutes: 30
4732-
steps:
4733-
- name: Checkout repo @ SHA - ${{ github.sha }}
4734-
uses: actions/checkout@v4
4735-
4736-
- name: Set up Python 3.12
4737-
uses: actions/setup-python@v5
4738-
with:
4739-
python-version: "3.12"
4740-
4741-
- name: Install tox
4742-
run: pip install tox-uv
4743-
4744-
- name: Run tests
4745-
run: tox -e py312-test-instrumentation-jinja2 -- -ra
4746-
4747-
py313-test-instrumentation-jinja2_ubuntu-latest:
4748-
name: instrumentation-jinja2 3.13 Ubuntu
4749-
runs-on: ubuntu-latest
4750-
timeout-minutes: 30
4751-
steps:
4752-
- name: Checkout repo @ SHA - ${{ github.sha }}
4753-
uses: actions/checkout@v4
4754-
4755-
- name: Set up Python 3.13
4756-
uses: actions/setup-python@v5
4757-
with:
4758-
python-version: "3.13"
4759-
4760-
- name: Install tox
4761-
run: pip install tox-uv
4762-
4763-
- name: Run tests
4764-
run: tox -e py313-test-instrumentation-jinja2 -- -ra
4765-
4766-
pypy3-test-instrumentation-jinja2_ubuntu-latest:
4767-
name: instrumentation-jinja2 pypy-3.9 Ubuntu
4768-
runs-on: ubuntu-latest
4769-
timeout-minutes: 30
4770-
steps:
4771-
- name: Checkout repo @ SHA - ${{ github.sha }}
4772-
uses: actions/checkout@v4
4773-
4774-
- name: Set up Python pypy-3.9
4775-
uses: actions/setup-python@v5
4776-
with:
4777-
python-version: "pypy-3.9"
4778-
4779-
- name: Install tox
4780-
run: pip install tox-uv
4781-
4782-
- name: Run tests
4783-
run: tox -e pypy3-test-instrumentation-jinja2 -- -ra

0 commit comments

Comments
 (0)