Skip to content

Commit ba43d31

Browse files
committed
Add OpenTelemetry instrumentation for MCP (Model Context Protocol)
This commit adds automatic instrumentation for the Model Context Protocol, enabling distributed tracing for MCP client and server operations. Key Features: - Automatic tracing for MCP client requests and notifications - Automatic tracing for MCP server request/notification handlers - Distributed trace context propagation via W3C Trace Context - Support for both stdio and HTTP transports - MCP semantic conventions support - Session ID tracking for HTTP transport Implementation: - Instruments BaseSession.send_request/send_notification (client-side) - Instruments Server._handle_request/_handle_notification (server-side) - Injects/extracts trace context via params._meta field - Creates linked spans for distributed tracing Code Quality: - 58 comprehensive unit tests (100% pass rate) - Lint score: 10.00/10 - Passes all pre-commit hooks - Full type annotations and documentation Package: opentelemetry-instrumentation-mcp Location: instrumentation-genai/opentelemetry-instrumentation-mcp/ Dependencies: mcp >= 1.8.1
1 parent f3d0394 commit ba43d31

38 files changed

+4461
-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)