rh-fastmcp-server-commons 0.3.1__tar.gz
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- rh_fastmcp_server_commons-0.3.1/LICENSE +204 -0
- rh_fastmcp_server_commons-0.3.1/PKG-INFO +58 -0
- rh_fastmcp_server_commons-0.3.1/README.pypi.md +33 -0
- rh_fastmcp_server_commons-0.3.1/docs/__init__.py +1 -0
- rh_fastmcp_server_commons-0.3.1/docs/authentication.md +190 -0
- rh_fastmcp_server_commons-0.3.1/docs/configuration.md +46 -0
- rh_fastmcp_server_commons-0.3.1/docs/graphql.md +132 -0
- rh_fastmcp_server_commons-0.3.1/docs/http-client.md +37 -0
- rh_fastmcp_server_commons-0.3.1/docs/observability.md +74 -0
- rh_fastmcp_server_commons-0.3.1/docs/search.md +98 -0
- rh_fastmcp_server_commons-0.3.1/pyproject.toml +102 -0
- rh_fastmcp_server_commons-0.3.1/rh_fastmcp_server_commons/__init__.py +1 -0
- rh_fastmcp_server_commons-0.3.1/rh_fastmcp_server_commons/auth/__init__.py +17 -0
- rh_fastmcp_server_commons-0.3.1/rh_fastmcp_server_commons/auth/mcp_auth.py +129 -0
- rh_fastmcp_server_commons-0.3.1/rh_fastmcp_server_commons/auth/scope_enforcement.py +574 -0
- rh_fastmcp_server_commons-0.3.1/rh_fastmcp_server_commons/env.py +57 -0
- rh_fastmcp_server_commons-0.3.1/rh_fastmcp_server_commons/logging_setup.py +113 -0
- rh_fastmcp_server_commons-0.3.1/rh_fastmcp_server_commons/middleware/__init__.py +0 -0
- rh_fastmcp_server_commons-0.3.1/rh_fastmcp_server_commons/middleware/request_logging.py +79 -0
- rh_fastmcp_server_commons-0.3.1/rh_fastmcp_server_commons/pipeline/__init__.py +15 -0
- rh_fastmcp_server_commons-0.3.1/rh_fastmcp_server_commons/pipeline/context.py +18 -0
- rh_fastmcp_server_commons-0.3.1/rh_fastmcp_server_commons/pipeline/errors.py +33 -0
- rh_fastmcp_server_commons-0.3.1/rh_fastmcp_server_commons/pipeline/handler.py +14 -0
- rh_fastmcp_server_commons-0.3.1/rh_fastmcp_server_commons/pipeline/handlers/__init__.py +0 -0
- rh_fastmcp_server_commons-0.3.1/rh_fastmcp_server_commons/pipeline/handlers/graphql.py +32 -0
- rh_fastmcp_server_commons-0.3.1/rh_fastmcp_server_commons/pipeline/handlers/transform.py +28 -0
- rh_fastmcp_server_commons-0.3.1/rh_fastmcp_server_commons/pipeline/runner.py +30 -0
- rh_fastmcp_server_commons-0.3.1/rh_fastmcp_server_commons/py.typed +0 -0
- rh_fastmcp_server_commons-0.3.1/rh_fastmcp_server_commons/schemas/__init__.py +3 -0
- rh_fastmcp_server_commons-0.3.1/rh_fastmcp_server_commons/schemas/errors.py +7 -0
- rh_fastmcp_server_commons-0.3.1/rh_fastmcp_server_commons/services/__init__.py +1 -0
- rh_fastmcp_server_commons-0.3.1/rh_fastmcp_server_commons/services/graphql/__init__.py +23 -0
- rh_fastmcp_server_commons-0.3.1/rh_fastmcp_server_commons/services/graphql/operations.py +168 -0
- rh_fastmcp_server_commons-0.3.1/rh_fastmcp_server_commons/services/graphql/query_loader.py +41 -0
- rh_fastmcp_server_commons-0.3.1/rh_fastmcp_server_commons/services/http/__init__.py +6 -0
- rh_fastmcp_server_commons-0.3.1/rh_fastmcp_server_commons/services/http/client.py +49 -0
- rh_fastmcp_server_commons-0.3.1/rh_fastmcp_server_commons/services/search/__init__.py +6 -0
- rh_fastmcp_server_commons-0.3.1/rh_fastmcp_server_commons/services/search/service.py +72 -0
- rh_fastmcp_server_commons-0.3.1/rh_fastmcp_server_commons/telemetry.py +66 -0
- rh_fastmcp_server_commons-0.3.1/rh_fastmcp_server_commons/tools/__init__.py +7 -0
- rh_fastmcp_server_commons-0.3.1/rh_fastmcp_server_commons/tools/common.py +39 -0
|
@@ -0,0 +1,204 @@
|
|
|
1
|
+
Copyright 2026 Red Hat, Inc.
|
|
2
|
+
|
|
3
|
+
|
|
4
|
+
Apache License
|
|
5
|
+
Version 2.0, January 2004
|
|
6
|
+
http://www.apache.org/licenses/
|
|
7
|
+
|
|
8
|
+
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
|
|
9
|
+
|
|
10
|
+
1. Definitions.
|
|
11
|
+
|
|
12
|
+
"License" shall mean the terms and conditions for use, reproduction,
|
|
13
|
+
and distribution as defined by Sections 1 through 9 of this document.
|
|
14
|
+
|
|
15
|
+
"Licensor" shall mean the copyright owner or entity authorized by
|
|
16
|
+
the copyright owner that is granting the License.
|
|
17
|
+
|
|
18
|
+
"Legal Entity" shall mean the union of the acting entity and all
|
|
19
|
+
other entities that control, are controlled by, or are under common
|
|
20
|
+
control with that entity. For the purposes of this definition,
|
|
21
|
+
"control" means (i) the power, direct or indirect, to cause the
|
|
22
|
+
direction or management of such entity, whether by contract or
|
|
23
|
+
otherwise, or (ii) ownership of fifty percent (50%) or more of the
|
|
24
|
+
outstanding shares, or (iii) beneficial ownership of such entity.
|
|
25
|
+
|
|
26
|
+
"You" (or "Your") shall mean an individual or Legal Entity
|
|
27
|
+
exercising permissions granted by this License.
|
|
28
|
+
|
|
29
|
+
"Source" form shall mean the preferred form for making modifications,
|
|
30
|
+
including but not limited to software source code, documentation
|
|
31
|
+
source, and configuration files.
|
|
32
|
+
|
|
33
|
+
"Object" form shall mean any form resulting from mechanical
|
|
34
|
+
transformation or translation of a Source form, including but
|
|
35
|
+
not limited to compiled object code, generated documentation,
|
|
36
|
+
and conversions to other media types.
|
|
37
|
+
|
|
38
|
+
"Work" shall mean the work of authorship, whether in Source or
|
|
39
|
+
Object form, made available under the License, as indicated by a
|
|
40
|
+
copyright notice that is included in or attached to the work
|
|
41
|
+
(an example is provided in the Appendix below).
|
|
42
|
+
|
|
43
|
+
"Derivative Works" shall mean any work, whether in Source or Object
|
|
44
|
+
form, that is based on (or derived from) the Work and for which the
|
|
45
|
+
editorial revisions, annotations, elaborations, or other modifications
|
|
46
|
+
represent, as a whole, an original work of authorship. For the purposes
|
|
47
|
+
of this License, Derivative Works shall not include works that remain
|
|
48
|
+
separable from, or merely link (or bind by name) to the interfaces of,
|
|
49
|
+
the Work and Derivative Works thereof.
|
|
50
|
+
|
|
51
|
+
"Contribution" shall mean any work of authorship, including
|
|
52
|
+
the original version of the Work and any modifications or additions
|
|
53
|
+
to that Work or Derivative Works thereof, that is intentionally
|
|
54
|
+
submitted to Licensor for inclusion in the Work by the copyright owner
|
|
55
|
+
or by an individual or Legal Entity authorized to submit on behalf of
|
|
56
|
+
the copyright owner. For the purposes of this definition, "submitted"
|
|
57
|
+
means any form of electronic, verbal, or written communication sent
|
|
58
|
+
to the Licensor or its representatives, including but not limited to
|
|
59
|
+
communication on electronic mailing lists, source code control systems,
|
|
60
|
+
and issue tracking systems that are managed by, or on behalf of, the
|
|
61
|
+
Licensor for the purpose of discussing and improving the Work, but
|
|
62
|
+
excluding communication that is conspicuously marked or otherwise
|
|
63
|
+
designated in writing by the copyright owner as "Not a Contribution."
|
|
64
|
+
|
|
65
|
+
"Contributor" shall mean Licensor and any individual or Legal Entity
|
|
66
|
+
on behalf of whom a Contribution has been received by Licensor and
|
|
67
|
+
subsequently incorporated within the Work.
|
|
68
|
+
|
|
69
|
+
2. Grant of Copyright License. Subject to the terms and conditions of
|
|
70
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
71
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
72
|
+
copyright license to reproduce, prepare Derivative Works of,
|
|
73
|
+
publicly display, publicly perform, sublicense, and distribute the
|
|
74
|
+
Work and such Derivative Works in Source or Object form.
|
|
75
|
+
|
|
76
|
+
3. Grant of Patent License. Subject to the terms and conditions of
|
|
77
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
78
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
79
|
+
(except as stated in this section) patent license to make, have made,
|
|
80
|
+
use, offer to sell, sell, import, and otherwise transfer the Work,
|
|
81
|
+
where such license applies only to those patent claims licensable
|
|
82
|
+
by such Contributor that are necessarily infringed by their
|
|
83
|
+
Contribution(s) alone or by combination of their Contribution(s)
|
|
84
|
+
with the Work to which such Contribution(s) was submitted. If You
|
|
85
|
+
institute patent litigation against any entity (including a
|
|
86
|
+
cross-claim or counterclaim in a lawsuit) alleging that the Work
|
|
87
|
+
or a Contribution incorporated within the Work constitutes direct
|
|
88
|
+
or contributory patent infringement, then any patent licenses
|
|
89
|
+
granted to You under this License for that Work shall terminate
|
|
90
|
+
as of the date such litigation is filed.
|
|
91
|
+
|
|
92
|
+
4. Redistribution. You may reproduce and distribute copies of the
|
|
93
|
+
Work or Derivative Works thereof in any medium, with or without
|
|
94
|
+
modifications, and in Source or Object form, provided that You
|
|
95
|
+
meet the following conditions:
|
|
96
|
+
|
|
97
|
+
(a) You must give any other recipients of the Work or
|
|
98
|
+
Derivative Works a copy of this License; and
|
|
99
|
+
|
|
100
|
+
(b) You must cause any modified files to carry prominent notices
|
|
101
|
+
stating that You changed the files; and
|
|
102
|
+
|
|
103
|
+
(c) You must retain, in the Source form of any Derivative Works
|
|
104
|
+
that You distribute, all copyright, patent, trademark, and
|
|
105
|
+
attribution notices from the Source form of the Work,
|
|
106
|
+
excluding those notices that do not pertain to any part of
|
|
107
|
+
the Derivative Works; and
|
|
108
|
+
|
|
109
|
+
(d) If the Work includes a "NOTICE" text file as part of its
|
|
110
|
+
distribution, then any Derivative Works that You distribute must
|
|
111
|
+
include a readable copy of the attribution notices contained
|
|
112
|
+
within such NOTICE file, excluding those notices that do not
|
|
113
|
+
pertain to any part of the Derivative Works, in at least one
|
|
114
|
+
of the following places: within a NOTICE text file distributed
|
|
115
|
+
as part of the Derivative Works; within the Source form or
|
|
116
|
+
documentation, if provided along with the Derivative Works; or,
|
|
117
|
+
within a display generated by the Derivative Works, if and
|
|
118
|
+
wherever such third-party notices normally appear. The contents
|
|
119
|
+
of the NOTICE file are for informational purposes only and
|
|
120
|
+
do not modify the License. You may add Your own attribution
|
|
121
|
+
notices within Derivative Works that You distribute, alongside
|
|
122
|
+
or as an addendum to the NOTICE text from the Work, provided
|
|
123
|
+
that such additional attribution notices cannot be construed
|
|
124
|
+
as modifying the License.
|
|
125
|
+
|
|
126
|
+
You may add Your own copyright statement to Your modifications and
|
|
127
|
+
may provide additional or different license terms and conditions
|
|
128
|
+
for use, reproduction, or distribution of Your modifications, or
|
|
129
|
+
for any such Derivative Works as a whole, provided Your use,
|
|
130
|
+
reproduction, and distribution of the Work otherwise complies with
|
|
131
|
+
the conditions stated in this License.
|
|
132
|
+
|
|
133
|
+
5. Submission of Contributions. Unless You explicitly state otherwise,
|
|
134
|
+
any Contribution intentionally submitted for inclusion in the Work
|
|
135
|
+
by You to the Licensor shall be under the terms and conditions of
|
|
136
|
+
this License, without any additional terms or conditions.
|
|
137
|
+
Notwithstanding the above, nothing herein shall supersede or modify
|
|
138
|
+
the terms of any separate license agreement you may have executed
|
|
139
|
+
with Licensor regarding such Contributions.
|
|
140
|
+
|
|
141
|
+
6. Trademarks. This License does not grant permission to use the trade
|
|
142
|
+
names, trademarks, service marks, or product names of the Licensor,
|
|
143
|
+
except as required for reasonable and customary use in describing the
|
|
144
|
+
origin of the Work and reproducing the content of the NOTICE file.
|
|
145
|
+
|
|
146
|
+
7. Disclaimer of Warranty. Unless required by applicable law or
|
|
147
|
+
agreed to in writing, Licensor provides the Work (and each
|
|
148
|
+
Contributor provides its Contributions) on an "AS IS" BASIS,
|
|
149
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
|
|
150
|
+
implied, including, without limitation, any warranties or conditions
|
|
151
|
+
of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
|
|
152
|
+
PARTICULAR PURPOSE. You are solely responsible for determining the
|
|
153
|
+
appropriateness of using or redistributing the Work and assume any
|
|
154
|
+
risks associated with Your exercise of permissions under this License.
|
|
155
|
+
|
|
156
|
+
8. Limitation of Liability. In no event and under no legal theory,
|
|
157
|
+
whether in tort (including negligence), contract, or otherwise,
|
|
158
|
+
unless required by applicable law (such as deliberate and grossly
|
|
159
|
+
negligent acts) or agreed to in writing, shall any Contributor be
|
|
160
|
+
liable to You for damages, including any direct, indirect, special,
|
|
161
|
+
incidental, or consequential damages of any character arising as a
|
|
162
|
+
result of this License or out of the use or inability to use the
|
|
163
|
+
Work (including but not limited to damages for loss of goodwill,
|
|
164
|
+
work stoppage, computer failure or malfunction, or any and all
|
|
165
|
+
other commercial damages or losses), even if such Contributor
|
|
166
|
+
has been advised of the possibility of such damages.
|
|
167
|
+
|
|
168
|
+
9. Accepting Warranty or Additional Liability. While redistributing
|
|
169
|
+
the Work or Derivative Works thereof, You may choose to offer,
|
|
170
|
+
and charge a fee for, acceptance of support, warranty, indemnity,
|
|
171
|
+
or other liability obligations and/or rights consistent with this
|
|
172
|
+
License. However, in accepting such obligations, You may act only
|
|
173
|
+
on Your own behalf and on Your sole responsibility, not on behalf
|
|
174
|
+
of any other Contributor, and only if You agree to indemnify,
|
|
175
|
+
defend, and hold each Contributor harmless for any liability
|
|
176
|
+
incurred by, or claims asserted against, such Contributor by reason
|
|
177
|
+
of your accepting any such warranty or additional liability.
|
|
178
|
+
|
|
179
|
+
END OF TERMS AND CONDITIONS
|
|
180
|
+
|
|
181
|
+
APPENDIX: How to apply the Apache License to your work.
|
|
182
|
+
|
|
183
|
+
To apply the Apache License to your work, attach the following
|
|
184
|
+
boilerplate notice, with the fields enclosed by brackets "[]"
|
|
185
|
+
replaced with your own identifying information. (Don't include
|
|
186
|
+
the brackets!) The text should be enclosed in the appropriate
|
|
187
|
+
comment syntax for the file format. We also recommend that a
|
|
188
|
+
file or class name and description of purpose be included on the
|
|
189
|
+
same "printed page" as the copyright notice for easier
|
|
190
|
+
identification within third-party archives.
|
|
191
|
+
|
|
192
|
+
Copyright [yyyy] [name of copyright owner]
|
|
193
|
+
|
|
194
|
+
Licensed under the Apache License, Version 2.0 (the "License");
|
|
195
|
+
you may not use this file except in compliance with the License.
|
|
196
|
+
You may obtain a copy of the License at
|
|
197
|
+
|
|
198
|
+
http://www.apache.org/licenses/LICENSE-2.0
|
|
199
|
+
|
|
200
|
+
Unless required by applicable law or agreed to in writing, software
|
|
201
|
+
distributed under the License is distributed on an "AS IS" BASIS,
|
|
202
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
203
|
+
See the License for the specific language governing permissions and
|
|
204
|
+
limitations under the License.
|
|
@@ -0,0 +1,58 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: rh-fastmcp-server-commons
|
|
3
|
+
Version: 0.3.1
|
|
4
|
+
Summary: Shared FastMCP scaffolding for Red Hat MCP servers.
|
|
5
|
+
License: Apache-2.0
|
|
6
|
+
License-File: LICENSE
|
|
7
|
+
Author: Red Hat Lightforge MCP
|
|
8
|
+
Author-email: lightforge-mcp-eng@redhat.com
|
|
9
|
+
Maintainer: Red Hat Lightforge MCP
|
|
10
|
+
Maintainer-email: lightforge-mcp-eng@redhat.com
|
|
11
|
+
Requires-Python: >=3.12,<4.0
|
|
12
|
+
Classifier: License :: OSI Approved :: Apache Software License
|
|
13
|
+
Classifier: Programming Language :: Python :: 3
|
|
14
|
+
Classifier: Programming Language :: Python :: 3.12
|
|
15
|
+
Classifier: Programming Language :: Python :: 3.13
|
|
16
|
+
Classifier: Programming Language :: Python :: 3.14
|
|
17
|
+
Requires-Dist: certifi (>=2024.0.0)
|
|
18
|
+
Requires-Dist: fastmcp (>=3.4.4,<4.0.0)
|
|
19
|
+
Requires-Dist: httpx (>=0.28.1,<0.29.0)
|
|
20
|
+
Requires-Dist: opentelemetry-exporter-otlp-proto-grpc (>=1.20.0,<2.0.0)
|
|
21
|
+
Requires-Dist: opentelemetry-sdk (>=1.20.0,<2.0.0)
|
|
22
|
+
Requires-Dist: pydantic (>=2.0,<3.0)
|
|
23
|
+
Description-Content-Type: text/markdown
|
|
24
|
+
|
|
25
|
+
# rh-fastmcp-server-commons
|
|
26
|
+
|
|
27
|
+
Shared FastMCP scaffolding for Red Hat MCP servers: MCP Authorization spec compliant setup, correct scopes step-up handling and per-tool scopes restriction, tool pipeline, GraphQL operations, HTTP/TLS helpers, search client, and observability setup.
|
|
28
|
+
|
|
29
|
+
Developed by **Red Hat** as part of the **Lightforge** MCP initiative.
|
|
30
|
+
|
|
31
|
+
Source will be open-sourced later. The package is public on PyPI now, the VCS is not public yet.
|
|
32
|
+
|
|
33
|
+
## Install
|
|
34
|
+
|
|
35
|
+
```bash
|
|
36
|
+
pip install rh-fastmcp-server-commons
|
|
37
|
+
```
|
|
38
|
+
|
|
39
|
+
```python
|
|
40
|
+
import rh_fastmcp_server_commons
|
|
41
|
+
from rh_fastmcp_server_commons.auth import build_auth_provider, build_http_app
|
|
42
|
+
```
|
|
43
|
+
|
|
44
|
+
## Documentation
|
|
45
|
+
|
|
46
|
+
Library usage docs ship with the installed package under `rh_fastmcp_server_commons/docs/`:
|
|
47
|
+
|
|
48
|
+
- `configuration.md`
|
|
49
|
+
- `authentication.md`
|
|
50
|
+
- `graphql.md`
|
|
51
|
+
- `http-client.md`
|
|
52
|
+
- `observability.md`
|
|
53
|
+
- `search.md`
|
|
54
|
+
|
|
55
|
+
## License
|
|
56
|
+
|
|
57
|
+
Apache-2.0. See the `LICENSE` file included in the distribution.
|
|
58
|
+
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
# rh-fastmcp-server-commons
|
|
2
|
+
|
|
3
|
+
Shared FastMCP scaffolding for Red Hat MCP servers: MCP Authorization spec compliant setup, correct scopes step-up handling and per-tool scopes restriction, tool pipeline, GraphQL operations, HTTP/TLS helpers, search client, and observability setup.
|
|
4
|
+
|
|
5
|
+
Developed by **Red Hat** as part of the **Lightforge** MCP initiative.
|
|
6
|
+
|
|
7
|
+
Source will be open-sourced later. The package is public on PyPI now, the VCS is not public yet.
|
|
8
|
+
|
|
9
|
+
## Install
|
|
10
|
+
|
|
11
|
+
```bash
|
|
12
|
+
pip install rh-fastmcp-server-commons
|
|
13
|
+
```
|
|
14
|
+
|
|
15
|
+
```python
|
|
16
|
+
import rh_fastmcp_server_commons
|
|
17
|
+
from rh_fastmcp_server_commons.auth import build_auth_provider, build_http_app
|
|
18
|
+
```
|
|
19
|
+
|
|
20
|
+
## Documentation
|
|
21
|
+
|
|
22
|
+
Library usage docs ship with the installed package under `rh_fastmcp_server_commons/docs/`:
|
|
23
|
+
|
|
24
|
+
- `configuration.md`
|
|
25
|
+
- `authentication.md`
|
|
26
|
+
- `graphql.md`
|
|
27
|
+
- `http-client.md`
|
|
28
|
+
- `observability.md`
|
|
29
|
+
- `search.md`
|
|
30
|
+
|
|
31
|
+
## License
|
|
32
|
+
|
|
33
|
+
Apache-2.0. See the `LICENSE` file included in the distribution.
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
"""Packaging marker so Poetry can ship this tree as ``rh_fastmcp_server_commons.docs``."""
|
|
@@ -0,0 +1,190 @@
|
|
|
1
|
+
# Authentication and authorization
|
|
2
|
+
|
|
3
|
+
Wire two layers together:
|
|
4
|
+
|
|
5
|
+
1. **`build_auth_provider()`** - OAuth/JWT validation and FastMCP's global gate (`RequireAuthMiddleware`)
|
|
6
|
+
2. **`build_http_app(mcp)`** - `ScopeEnforcementMiddleware` for RFC 6750 `scope=` on 401/403 and optional per-tool scope step-up
|
|
7
|
+
|
|
8
|
+
## Wire an authenticated MCP server
|
|
9
|
+
|
|
10
|
+
```python
|
|
11
|
+
from fastmcp import FastMCP
|
|
12
|
+
from rh_fastmcp_server_commons.auth import (
|
|
13
|
+
build_auth_provider,
|
|
14
|
+
build_http_app,
|
|
15
|
+
require_tool_scopes,
|
|
16
|
+
)
|
|
17
|
+
|
|
18
|
+
mcp = FastMCP("my-server", auth=build_auth_provider())
|
|
19
|
+
|
|
20
|
+
@mcp.tool
|
|
21
|
+
@require_tool_scopes("api.security") # optional; under @mcp.tool
|
|
22
|
+
async def search_advisories(...):
|
|
23
|
+
...
|
|
24
|
+
|
|
25
|
+
@mcp.tool
|
|
26
|
+
@require_tool_scopes("api.security", "api.write") # AND: need both
|
|
27
|
+
async def update_advisory(...):
|
|
28
|
+
...
|
|
29
|
+
|
|
30
|
+
@mcp.tool
|
|
31
|
+
async def list_products(...): # global AUTH_REQUIRED_SCOPES only
|
|
32
|
+
...
|
|
33
|
+
|
|
34
|
+
app = build_http_app(mcp) # not mcp.http_app() / bare mcp.run()
|
|
35
|
+
```
|
|
36
|
+
|
|
37
|
+
Register all tools before `build_http_app`. Decorator order: `@require_tool_scopes` under `@mcp.tool` (reverse raises `TypeError`).
|
|
38
|
+
|
|
39
|
+
### Request middleware order
|
|
40
|
+
|
|
41
|
+
```text
|
|
42
|
+
RequestContextMiddleware
|
|
43
|
+
-> AuthenticationMiddleware # JWT -> scope["user"]
|
|
44
|
+
-> AuthContextMiddleware
|
|
45
|
+
-> ScopeEnforcementMiddleware # ours: scope= on 401/403, per-tool 403 step-up
|
|
46
|
+
-> RequireAuthMiddleware # FastMCP: global AUTH_REQUIRED_SCOPES
|
|
47
|
+
-> MCP JSON-RPC handler
|
|
48
|
+
```
|
|
49
|
+
|
|
50
|
+
`ScopeEnforcementMiddleware` does not re-check global scopes; it advertises the same `mcp.auth.required_scopes` list that `RequireAuthMiddleware` enforces.
|
|
51
|
+
|
|
52
|
+
### `build_http_app` API
|
|
53
|
+
|
|
54
|
+
```python
|
|
55
|
+
def build_http_app(
|
|
56
|
+
mcp: FastMCP,
|
|
57
|
+
*,
|
|
58
|
+
middleware: list[Middleware] | None = None,
|
|
59
|
+
tool_scopes: dict[str, list[str]] | None = None,
|
|
60
|
+
**kwargs: Any,
|
|
61
|
+
) -> StarletteWithLifespan: ...
|
|
62
|
+
```
|
|
63
|
+
|
|
64
|
+
| Argument | Meaning |
|
|
65
|
+
|----------|---------|
|
|
66
|
+
| `mcp` | Tools must already be registered |
|
|
67
|
+
| `middleware` | Extra middleware after scope enforcement; must not include `ScopeEnforcementMiddleware` |
|
|
68
|
+
| `tool_scopes` | Merge/override of per-tool scopes (explicit wins). Keys must be registered tool names |
|
|
69
|
+
| `**kwargs` | Passed to `mcp.http_app()` (`path=`, `json_response=`, …) |
|
|
70
|
+
|
|
71
|
+
No `global_scopes=` kwarg (use `AUTH_REQUIRED_SCOPES`). Prepends `ScopeEnforcementMiddleware` when global and/or per-tool scopes exist; otherwise installs nothing.
|
|
72
|
+
|
|
73
|
+
For a fully custom stack without `build_http_app`, use `build_scope_enforcement_middleware(mcp)` + `mcp.http_app(middleware=[scope_mw, ...])`.
|
|
74
|
+
|
|
75
|
+
`FastMCP.http_app` / `mcp.run()` raise if scope middleware is required but missing (`required_scopes` and/or `@require_tool_scopes`), or if per-tool scopes are set without `mcp.auth`. Also rejected: empty `@require_tool_scopes()` / empty meta scopes, meta vs decorator conflict, unknown `tool_scopes=` keys, wrong decorator order, duplicate scope middleware in `build_http_app(..., middleware=...)`.
|
|
76
|
+
|
|
77
|
+
## Authenticate callers (OAuth / JWT)
|
|
78
|
+
|
|
79
|
+
`build_auth_provider()` enables:
|
|
80
|
+
|
|
81
|
+
1. **Protected Resource Metadata (RFC 9728)** at `/.well-known/oauth-protected-resource/mcp` when the MCP path is `/mcp` (`resource` from `AUTH_RESOURCE` / `MCP_BASE_URL`, `authorization_servers` from `AUTH_SERVER`, `scopes_supported` from `AUTH_REQUIRED_SCOPES`, `bearer_methods_supported: ["header"]`). `AUTH_AUDIENCE` and `AUTH_ISSUER` are JWT checks only; they do not appear in PRM.
|
|
82
|
+
2. **JWT validation**: signature (JWKS), `exp`, optional `iss` / `aud`
|
|
83
|
+
|
|
84
|
+
### Environment variables
|
|
85
|
+
|
|
86
|
+
Full catalog and bootstrap order: [configuration](configuration.md). Auth-critical vars:
|
|
87
|
+
|
|
88
|
+
| Variable | Role |
|
|
89
|
+
|----------|------|
|
|
90
|
+
| `AUTH_SERVER` | Required. MCP Auth IdP/SSO URL (OAuth/DCR, metadata, PRM `authorization_servers`). Raises if unset in `build_auth_provider()`. |
|
|
91
|
+
| `AUTH_ISSUER` | Optional expected JWT `iss` (unset skips check) |
|
|
92
|
+
| `AUTH_JWKS_URI` | Optional JWKS override; else discovered from `AUTH_SERVER` |
|
|
93
|
+
| `AUTH_AUDIENCE` | Optional expected `aud` (comma-separated). Unset skips + warns |
|
|
94
|
+
| `AUTH_REQUIRED_SCOPES` | Optional global scopes (also PRM `scopes_supported`) |
|
|
95
|
+
| `AUTH_RESOURCE` | Optional public MCP resource URL for PRM; else local/deploy fallback via `LIGHTRAIL_ROUTE_*` |
|
|
96
|
+
|
|
97
|
+
JWKS fetch uses the shared async httpx client; authorization-server metadata uses a short-lived sync client with the same TLS settings ([HTTP client](http-client.md)).
|
|
98
|
+
|
|
99
|
+
### Example configuration
|
|
100
|
+
|
|
101
|
+
```bash
|
|
102
|
+
AUTH_SERVER=https://auth.example.com
|
|
103
|
+
AUTH_ISSUER=
|
|
104
|
+
AUTH_AUDIENCE=mcp.read
|
|
105
|
+
AUTH_REQUIRED_SCOPES=mcp.read
|
|
106
|
+
AUTH_RESOURCE=https://mcp.example.com/mcp
|
|
107
|
+
```
|
|
108
|
+
|
|
109
|
+
## Authorize every tool with global scopes
|
|
110
|
+
|
|
111
|
+
Set `AUTH_REQUIRED_SCOPES` (via `build_auth_provider()` → `mcp.auth.required_scopes`). FastMCP `RequireAuthMiddleware` rejects tokens that lack those scopes.
|
|
112
|
+
|
|
113
|
+
With `build_http_app(mcp)`, failed global checks get `WWW-Authenticate` with RFC 6750 `scope=` listing those global scopes (upstream FastMCP/SDK often omit `scope=`). That enables client step-up for the baseline server requirements.
|
|
114
|
+
|
|
115
|
+
Global scopes apply to all tools. Tools with no per-tool marks still need only this set (for example `mcp.read`).
|
|
116
|
+
|
|
117
|
+
## Enforce extra scopes per tool
|
|
118
|
+
|
|
119
|
+
Use `@require_tool_scopes` (or `meta["required_scopes"]`) when a tool needs scopes beyond the global set. Missing scopes → HTTP 403 with `WWW-Authenticate` and `scope=` = **union** of global + tool scopes (required set for that call, not only missing ones).
|
|
120
|
+
|
|
121
|
+
```python
|
|
122
|
+
@mcp.tool
|
|
123
|
+
@require_tool_scopes("api.security")
|
|
124
|
+
async def search_advisories(...):
|
|
125
|
+
...
|
|
126
|
+
```
|
|
127
|
+
|
|
128
|
+
Or override / merge at app build time:
|
|
129
|
+
|
|
130
|
+
```python
|
|
131
|
+
app = build_http_app(
|
|
132
|
+
mcp,
|
|
133
|
+
tool_scopes={"dynamic_tool": ["admin"]}, # tool must already exist on mcp
|
|
134
|
+
middleware=[my_custom_middleware],
|
|
135
|
+
path="/mcp",
|
|
136
|
+
)
|
|
137
|
+
```
|
|
138
|
+
|
|
139
|
+
### Step-up when a tool needs more scopes
|
|
140
|
+
|
|
141
|
+
1. Token has `mcp.read` but not `api.security` → global gate passes
|
|
142
|
+
2. Per-tool check fails → HTTP 403 with `scope="mcp.read api.security"`
|
|
143
|
+
3. Client re-authorizes with those scopes and retries
|
|
144
|
+
|
|
145
|
+
This is **not** FastMCP `auth=require_scopes(...)` (that hides tools and returns JSON-RPC "not found" on HTTP 200). Ours keeps tools listed and returns HTTP 403 + `WWW-Authenticate` for OAuth step-up. Both are valid; only ours is the Auth step-up path (see [compliance](#meet-mcp-authorization-requirements)).
|
|
146
|
+
|
|
147
|
+
### Cost of per-tool checks
|
|
148
|
+
|
|
149
|
+
- **No per-tool scopes**: response interception only (no body read); still patches `scope=` on global 401/403
|
|
150
|
+
- **Per-tool scopes**: buffer POST for `tools/call`; reject over `MAX_BODY_SIZE` (256 KB) with HTTP 413 and `Connection: close` (mid-stream rejects also drain a bounded amount of leftover body). Still set reverse-proxy limits (`client_max_body_size 256k` / ingress equivalent)
|
|
151
|
+
- **Streamable HTTP**: full step-up support. **SSE**: middleware runs; 403 on POST is unreliable for clients (protocol limitation). **STDIO**: N/A
|
|
152
|
+
|
|
153
|
+
## Use the access token in tools
|
|
154
|
+
|
|
155
|
+
```python
|
|
156
|
+
from fastmcp.server.dependencies import get_access_token
|
|
157
|
+
|
|
158
|
+
access_token = get_access_token()
|
|
159
|
+
if access_token:
|
|
160
|
+
raw_jwt = access_token.token
|
|
161
|
+
claims = access_token.claims
|
|
162
|
+
scopes = access_token.scopes
|
|
163
|
+
```
|
|
164
|
+
|
|
165
|
+
Bearer string only: `await resolve_bearer_token()` from `rh_fastmcp_server_commons.services.graphql`.
|
|
166
|
+
|
|
167
|
+
GraphQL forwarding (do not hit `GRAPHQL_ENDPOINT` with raw httpx if you need the MCP bearer):
|
|
168
|
+
|
|
169
|
+
- `execute_graphql(...)` / `run_operation(...)` / `ops, run_operation_fn = bind_operations(...)` / `GraphQLHandler`
|
|
170
|
+
|
|
171
|
+
GraphQL-backed MCP tools are still Python `@mcp.tool` wrappers. Per-tool scopes go on that Python tool. See [GraphQL tools](graphql.md).
|
|
172
|
+
|
|
173
|
+
## Meet MCP Authorization requirements
|
|
174
|
+
|
|
175
|
+
With `auth=build_auth_provider()` and `build_http_app(mcp)`:
|
|
176
|
+
|
|
177
|
+
| Feature | Spec / RFC | Status |
|
|
178
|
+
|---------|------------|--------|
|
|
179
|
+
| Protected Resource Metadata | RFC 9728 / MCP Auth | Yes (`build_auth_provider`) |
|
|
180
|
+
| Bearer + JWT sig/exp | MCP Auth / RFC 6750 | Yes |
|
|
181
|
+
| Global required scopes + PRM `scopes_supported` | MCP Auth | Yes (`AUTH_REQUIRED_SCOPES`) |
|
|
182
|
+
| `WWW-Authenticate` + `scope=` on 401/403 | RFC 6750 / MCP step-up | Yes via `build_http_app` (upstream FastMCP/SDK omit `scope=`) |
|
|
183
|
+
| Per-tool HTTP 403 step-up | MCP step-up / RFC 6750 | Yes via `@require_tool_scopes` (not via FastMCP `auth=require_scopes`) |
|
|
184
|
+
| Challenge `scope=` = required scopes for the operation | RFC 6750 / MCP Auth | Yes (global ∪ per-tool) |
|
|
185
|
+
| Audience binding | MCP Auth MUST (RFC 8707 intent) | **Configure `AUTH_AUDIENCE`**. Some IdPs put scopes in `aud`, not the resource URL. Unset = skipped + warning (violation) |
|
|
186
|
+
| Optional `iss` check | Defense in depth | Optional `AUTH_ISSUER` |
|
|
187
|
+
|
|
188
|
+
FastMCP `auth=require_scopes(...)` (hide tool / JSON-RPC not-found) is allowed and orthogonal; it is not the MCP Auth step-up challenge. Prefer `@require_tool_scopes` when clients must re-authorize.
|
|
189
|
+
|
|
190
|
+
Upstream: if FastMCP/SDK grow native `scope=` ([#4513](https://github.com/PrefectHQ/fastmcp/issues/4513)) or per-tool HTTP scopes ([#4334](https://github.com/PrefectHQ/fastmcp/issues/4334)), our middleware pass-through / thin-alias paths apply.
|
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
# Configuration
|
|
2
|
+
|
|
3
|
+
Shared environment variables live in `rh_fastmcp_server_commons.env`. Most values are read at **import time**, so servers must set defaults before importing consumers.
|
|
4
|
+
|
|
5
|
+
## Bootstrap order
|
|
6
|
+
|
|
7
|
+
1. `app/env_config.py` sets defaults on `os.environ`
|
|
8
|
+
2. Call `configure_telemetry()` before importing FastMCP (see [observability](observability.md))
|
|
9
|
+
3. Import auth, tools, and other modules that pull in `rh_fastmcp_server_commons.env`
|
|
10
|
+
4. Register tools, then `build_http_app(mcp)` (see [authentication](authentication.md))
|
|
11
|
+
|
|
12
|
+
## Resource URL resolution
|
|
13
|
+
|
|
14
|
+
`AUTH_RESOURCE` (also exported as `MCP_RESOURCE`) is the public MCP resource URL for OAuth PRM:
|
|
15
|
+
|
|
16
|
+
1. Use `AUTH_RESOURCE` if set
|
|
17
|
+
2. Else `LIGHTRAIL_ROUTE_SCHEME` (default `http`) + `LIGHTRAIL_ROUTE_HOSTNAME` (default `localhost:8080`) → `{scheme}://{host}/mcp`
|
|
18
|
+
|
|
19
|
+
`MCP_BASE_URL` is derived from that resource: strip a trailing `/mcp`, or use `scheme://netloc` when the path is not `/mcp`.
|
|
20
|
+
|
|
21
|
+
## Environment catalog
|
|
22
|
+
|
|
23
|
+
| Variable | Role | Details |
|
|
24
|
+
|----------|------|---------|
|
|
25
|
+
| `GRAPHQL_ENDPOINT` | Upstream GraphQL URL | [GraphQL tools](graphql.md) |
|
|
26
|
+
| `CLIENT_NAME` | `apollographql-client-name` header | [GraphQL tools](graphql.md) |
|
|
27
|
+
| `GRAPH_REF` | Optional Apollo graph ref (server use) | server-specific |
|
|
28
|
+
| `AUTH_SERVER` | MCP Auth IdP / SSO base URL | [Authentication](authentication.md) |
|
|
29
|
+
| `AUTH_ISSUER` | Optional expected JWT `iss` | [Authentication](authentication.md) |
|
|
30
|
+
| `AUTH_JWKS_URI` | Optional JWKS URL override | [Authentication](authentication.md) |
|
|
31
|
+
| `AUTH_AUDIENCE` | Optional expected JWT `aud` (comma-separated) | [Authentication](authentication.md) |
|
|
32
|
+
| `AUTH_REQUIRED_SCOPES` | Global required scopes (comma-separated) | [Authentication](authentication.md) |
|
|
33
|
+
| `AUTH_RESOURCE` | Public MCP resource URL for PRM | [Authentication](authentication.md), above |
|
|
34
|
+
| `LIGHTRAIL_ROUTE_SCHEME` / `LIGHTRAIL_ROUTE_HOSTNAME` | Local fallback for `AUTH_RESOURCE` | above |
|
|
35
|
+
| `LOGGING_LEVEL` | Root log level (default `info`) | [Observability](observability.md) |
|
|
36
|
+
| `OTEL_EXPORTER_OTLP_ENDPOINT` | Enables OTLP export when set | [Observability](observability.md) |
|
|
37
|
+
| `OTEL_SERVICE_NAME` | OTEL `service.name` | [Observability](observability.md) |
|
|
38
|
+
| `OTEL_RESOURCE_ATTRIBUTES` | Extra OTEL resource attrs (`k=v,k=v`); typically `deployment.environment=...` | [Observability](observability.md) |
|
|
39
|
+
| `OTEL_EXPORTER_OTLP_INSECURE` | Force insecure OTLP gRPC | [Observability](observability.md) |
|
|
40
|
+
| `SSL_VERIFY` | TLS verify for httpx (default `true`) | [HTTP client](http-client.md) |
|
|
41
|
+
| `EXTRA_CA_CERTS` / `SSL_CERT_FILE` | Optional extra CA bundle | [HTTP client](http-client.md) |
|
|
42
|
+
| `SEARCH_ENDPOINT` | Solr search URL | [Search](search.md) |
|
|
43
|
+
| `SEARCH_CLIENT_ID` | Parameter for search API call | [Search](search.md) |
|
|
44
|
+
| `LIGHTRAIL_CMDB_CODE` | Optional CMDB field on request logs | [Observability](observability.md) |
|
|
45
|
+
|
|
46
|
+
Missing `GRAPHQL_ENDPOINT` or `AUTH_SERVER` is logged as an error when `rh_fastmcp_server_commons.env` is imported; `build_auth_provider()` still raises if `AUTH_SERVER` is unset.
|
|
@@ -0,0 +1,132 @@
|
|
|
1
|
+
# GraphQL tools
|
|
2
|
+
|
|
3
|
+
Load named operations from `*.graphql`, POST them to `GRAPHQL_ENDPOINT` with the MCP bearer, and run them through a handler pipeline. MCP **tools** are Python `@mcp.tool` wrappers in each server repo. Domain queries, descriptions, and output schemas stay in the server (not this package). `.graphql` files are not tool definitions and carry no auth scopes.
|
|
4
|
+
|
|
5
|
+
## Server layout and flow
|
|
6
|
+
|
|
7
|
+
| Path | Role |
|
|
8
|
+
|------|------|
|
|
9
|
+
| `app/tools/operations/*.graphql` | Query + operation name (`bind_operations`) |
|
|
10
|
+
| `app/tools/descriptions/<tool>.md` | Tool description (`load_tool_description`) |
|
|
11
|
+
| `app/tools/__init__.py` | `OPERATIONS_DIR`, `DESCRIPTIONS_DIR` |
|
|
12
|
+
| `app/tools/pipeline_handlers/defaults.py` | `bind_operations` → shared handlers (`GRAPHQL_HANDLER`, …) |
|
|
13
|
+
| `app/tools/<tool>.py` | `OPERATION`, `HANDLERS`, `register(mcp)` |
|
|
14
|
+
| `app/tools/tools.py` | `register_tools(mcp)` |
|
|
15
|
+
| `app/tools/_common.py` | Shim: one-arg `load_tool_description`; re-exports `run_tool` / `output_schema` |
|
|
16
|
+
|
|
17
|
+
```text
|
|
18
|
+
operations/*.graphql
|
|
19
|
+
-> bind_operations(OPERATIONS_DIR) -> (OPS, run_operation)
|
|
20
|
+
-> defaults.py: GRAPHQL_HANDLER = GraphQLHandler(run_operation_fn=run_operation)
|
|
21
|
+
-> <tool>.py: register(mcp) / run_tool(..., HANDLERS)
|
|
22
|
+
-> optional pre-handlers (e.g. coerce search input)
|
|
23
|
+
-> GraphQLHandler fills ctx.raw
|
|
24
|
+
-> optional TransformHandler: ctx.raw -> ctx.payload
|
|
25
|
+
-> model_validate(ctx.payload or ctx.raw)
|
|
26
|
+
```
|
|
27
|
+
|
|
28
|
+
## Environment
|
|
29
|
+
|
|
30
|
+
Full catalog and bootstrap order: [configuration](configuration.md). GraphQL-critical vars:
|
|
31
|
+
|
|
32
|
+
| Variable | Role |
|
|
33
|
+
|----------|------|
|
|
34
|
+
| `GRAPHQL_ENDPOINT` | Upstream URL. Missing → `execute_graphql` returns `GraphQLResult` with `status_code=0` |
|
|
35
|
+
| `CLIENT_NAME` | `apollographql-client-name` (default `mcp-server`) |
|
|
36
|
+
|
|
37
|
+
Inbound MCP auth (`AUTH_*`) and the same bearer forwarded outbound: [authentication](authentication.md). TLS / httpx verify: [HTTP client](http-client.md).
|
|
38
|
+
|
|
39
|
+
## Operations (`*.graphql`)
|
|
40
|
+
|
|
41
|
+
```python
|
|
42
|
+
# defaults.py
|
|
43
|
+
from rh_fastmcp_server_commons.pipeline.handlers.graphql import GraphQLHandler
|
|
44
|
+
from rh_fastmcp_server_commons.services.graphql.operations import bind_operations
|
|
45
|
+
|
|
46
|
+
from app.tools import OPERATIONS_DIR
|
|
47
|
+
|
|
48
|
+
OPS, run_operation = bind_operations(OPERATIONS_DIR)
|
|
49
|
+
GRAPHQL_HANDLER = GraphQLHandler(run_operation_fn=run_operation)
|
|
50
|
+
# plus server-specific handlers as needed
|
|
51
|
+
```
|
|
52
|
+
|
|
53
|
+
Rules:
|
|
54
|
+
|
|
55
|
+
- One operation per file; name from `query operation_name` (not the filename)
|
|
56
|
+
- Duplicate names or an empty directory raise `ValueError`
|
|
57
|
+
- Stored as `GraphQLOperation(name, query)` only
|
|
58
|
+
|
|
59
|
+
Convention: same snake_case for file stem, GraphQL op, and MCP tool (e.g. `search_advisories`).
|
|
60
|
+
|
|
61
|
+
```graphql
|
|
62
|
+
query search_advisories($input: ErrataSearchInput!) {
|
|
63
|
+
# ...
|
|
64
|
+
}
|
|
65
|
+
```
|
|
66
|
+
|
|
67
|
+
## Transport
|
|
68
|
+
|
|
69
|
+
| API | Behavior |
|
|
70
|
+
|-----|----------|
|
|
71
|
+
| `resolve_bearer_token()` | FastMCP access token string, or `None` (logs error) |
|
|
72
|
+
| `execute_graphql(...)` | POST to `GRAPHQL_ENDPOINT` with bearer (explicit or resolved). Always calls `inject(headers)` for W3C context (useful when an active span exists; see [observability](observability.md)). TLS via `httpx_verify_setting()` ([HTTP client](http-client.md)). Returns `GraphQLResult(data, errors, status_code)` without raising. Config/token/network failures use `status_code=0` |
|
|
73
|
+
| `run_operation(ops, name, vars)` | Drops `None` vars, calls `execute_graphql`, returns `data` or raises `GraphQLToolError` (errors, missing data, or HTTP `>= 400`) |
|
|
74
|
+
|
|
75
|
+
Do not call `GRAPHQL_ENDPOINT` with raw httpx if you need the MCP bearer forwarded.
|
|
76
|
+
|
|
77
|
+
## Tool pipeline
|
|
78
|
+
|
|
79
|
+
`ToolContext`: `operation`, `tool_args`, `variables`, `model_type`, optional `raw` / `payload`.
|
|
80
|
+
|
|
81
|
+
| Handler | Role |
|
|
82
|
+
|---------|------|
|
|
83
|
+
| `GraphQLHandler` | Sets `ctx.raw` via `run_operation_fn`; maps `GraphQLToolError` → `ToolChainError` |
|
|
84
|
+
| `TransformHandler(fn)` | After GraphQL: requires `ctx.raw`; sets `ctx.payload = fn(ctx.raw, ctx.tool_args)` |
|
|
85
|
+
| Server-specific | Before and/or after GraphQL (e.g. coerce search `input`) |
|
|
86
|
+
|
|
87
|
+
`run_tool` → `run_tool_chain`: run handlers in order; validate `ctx.payload` or `ctx.raw` with `model_type`; on `ToolChainError` return FastMCP `ToolResult` (`is_error=True`, structured `ToolErrorResponse`). `run_tool` is generic over the output model (`T | ToolResult`), so servers do not need to cast the return type.
|
|
88
|
+
|
|
89
|
+
Commons helpers: `load_tool_description(name, dir)`, `output_schema(model)` (`model_json_schema()`), generic `run_tool`. Servers usually call them through `_common.py` (one-arg description loader plus re-exports).
|
|
90
|
+
|
|
91
|
+
## Defining a tool
|
|
92
|
+
|
|
93
|
+
```python
|
|
94
|
+
from fastmcp import FastMCP
|
|
95
|
+
from fastmcp.tools.base import ToolResult
|
|
96
|
+
from rh_fastmcp_server_commons.pipeline.handlers.transform import TransformHandler
|
|
97
|
+
|
|
98
|
+
from app.schemas.tool_outputs import GetProductIdOutput
|
|
99
|
+
from app.tools._common import load_tool_description, output_schema, run_tool
|
|
100
|
+
from app.tools.pipeline_handlers.defaults import GRAPHQL_HANDLER
|
|
101
|
+
from app.transformers.get_product_id import transform_get_product_id
|
|
102
|
+
|
|
103
|
+
OPERATION = "get_product_id"
|
|
104
|
+
HANDLERS = (
|
|
105
|
+
GRAPHQL_HANDLER,
|
|
106
|
+
TransformHandler(transform_get_product_id), # fn(raw, tool_args) -> dict for model_validate
|
|
107
|
+
)
|
|
108
|
+
# Optional: put server-specific handlers before GraphQL (e.g. SEARCH_INPUT_HANDLER)
|
|
109
|
+
# Omit TransformHandler when the GraphQL shape already matches the output model
|
|
110
|
+
|
|
111
|
+
|
|
112
|
+
def register(mcp: FastMCP) -> None:
|
|
113
|
+
@mcp.tool(
|
|
114
|
+
name=OPERATION,
|
|
115
|
+
description=load_tool_description(OPERATION),
|
|
116
|
+
output_schema=output_schema(GetProductIdOutput),
|
|
117
|
+
)
|
|
118
|
+
async def get_product_id(name: str | None = None) -> GetProductIdOutput | ToolResult:
|
|
119
|
+
return await run_tool(
|
|
120
|
+
OPERATION, {"name": name}, GetProductIdOutput, HANDLERS
|
|
121
|
+
)
|
|
122
|
+
```
|
|
123
|
+
|
|
124
|
+
```python
|
|
125
|
+
def register_tools(mcp: FastMCP) -> None:
|
|
126
|
+
for tool in TOOLS:
|
|
127
|
+
tool.register(mcp)
|
|
128
|
+
```
|
|
129
|
+
|
|
130
|
+
`run_tool`'s operation name must match the `query` identifier. `name=OPERATION` keeps MCP tool name and GraphQL op aligned when they share that string.
|
|
131
|
+
|
|
132
|
+
Per-tool OAuth scopes (if any) go on the Python `@mcp.tool`, not in `.graphql` or description files. Details: [authentication](authentication.md).
|