Or Weis commited on
Commit
9d765f1
·
unverified ·
1 Parent(s): c13df21

Permit.io integration docs (#1226)

Browse files
docs/docs.json CHANGED
@@ -65,10 +65,7 @@
65
  {
66
  "group": "Essentials",
67
  "icon": "cube",
68
- "pages": [
69
- "servers/server",
70
- "deployment/running-server"
71
- ]
72
  },
73
  {
74
  "group": "Core Components",
@@ -96,9 +93,7 @@
96
  {
97
  "group": "Authentication",
98
  "icon": "shield-check",
99
- "pages": [
100
- "servers/auth/bearer"
101
- ]
102
  }
103
  ]
104
  },
@@ -108,10 +103,7 @@
108
  {
109
  "group": "Essentials",
110
  "icon": "cube",
111
- "pages": [
112
- "clients/client",
113
- "clients/transports"
114
- ]
115
  },
116
  {
117
  "group": "Core Operations",
@@ -137,10 +129,7 @@
137
  {
138
  "group": "Authentication",
139
  "icon": "user-shield",
140
- "pages": [
141
- "clients/auth/oauth",
142
- "clients/auth/bearer"
143
- ]
144
  }
145
  ]
146
  },
@@ -158,6 +147,7 @@
158
  "integrations/mcp-json-configuration",
159
  "integrations/openai",
160
  "integrations/openapi",
 
161
  "integrations/starlette"
162
  ]
163
  },
@@ -185,17 +175,12 @@
185
  },
186
  {
187
  "anchor": "What's New",
188
- "pages": [
189
- "updates",
190
- "changelog"
191
- ]
192
  },
193
  {
194
  "anchor": "Community",
195
  "icon": "users",
196
- "pages": [
197
- "community/showcase"
198
- ]
199
  }
200
  ]
201
  },
 
65
  {
66
  "group": "Essentials",
67
  "icon": "cube",
68
+ "pages": ["servers/server", "deployment/running-server"]
 
 
 
69
  },
70
  {
71
  "group": "Core Components",
 
93
  {
94
  "group": "Authentication",
95
  "icon": "shield-check",
96
+ "pages": ["servers/auth/bearer"]
 
 
97
  }
98
  ]
99
  },
 
103
  {
104
  "group": "Essentials",
105
  "icon": "cube",
106
+ "pages": ["clients/client", "clients/transports"]
 
 
 
107
  },
108
  {
109
  "group": "Core Operations",
 
129
  {
130
  "group": "Authentication",
131
  "icon": "user-shield",
132
+ "pages": ["clients/auth/oauth", "clients/auth/bearer"]
 
 
 
133
  }
134
  ]
135
  },
 
147
  "integrations/mcp-json-configuration",
148
  "integrations/openai",
149
  "integrations/openapi",
150
+ "integrations/permit",
151
  "integrations/starlette"
152
  ]
153
  },
 
175
  },
176
  {
177
  "anchor": "What's New",
178
+ "pages": ["updates", "changelog"]
 
 
 
179
  },
180
  {
181
  "anchor": "Community",
182
  "icon": "users",
183
+ "pages": ["community/showcase"]
 
 
184
  }
185
  ]
186
  },
docs/integrations/images/abac_condition_example.png ADDED

Git LFS Details

  • SHA256: 7e6a58d8553c0c04859c7ab9e19a1f532d39fb711ea19fe1fb28ee69bbd9d074
  • Pointer size: 130 Bytes
  • Size of remote file: 83.7 kB
docs/integrations/images/abac_policy_example.png ADDED

Git LFS Details

  • SHA256: a322540446301ede4a3d2755515b5d0bb1503f407f2899aadea9b3d21fecd208
  • Pointer size: 130 Bytes
  • Size of remote file: 21.9 kB
docs/integrations/images/policy_mapping.png ADDED

Git LFS Details

  • SHA256: 61884af5c48c4e10ee7167726f996e2aceabd4dc718b7c9a451f85f7fd6838c6
  • Pointer size: 130 Bytes
  • Size of remote file: 16.4 kB
docs/integrations/images/role_assignement.png ADDED

Git LFS Details

  • SHA256: fdde86dfcc17bbb36678f9ded0d64c6ce312674d7bab03fd969c5456ccf0c760
  • Pointer size: 130 Bytes
  • Size of remote file: 74.3 kB
docs/integrations/permit.mdx ADDED
@@ -0,0 +1,353 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ ---
2
+ title: Permit.io Authorization 🤝 FastMCP
3
+ sidebarTitle: Permit.io Permissions
4
+ description: Add fine-grained authorization to your FastMCP servers with Permit.io
5
+ icon: shield-check
6
+ tag: NEW
7
+ ---
8
+
9
+ Add **policy-based authorization** to your FastMCP servers with one-line code addition with the **[Permit.io][permit-github] authorization middleware**.
10
+
11
+ Control which tools, resources and prompts MCP clients can view and execute on your server. Define dynamic policies using Permit.io's powerful RBAC, ABAC, and REBAC capabilities, and obtain comprehensive audit logs of all access attempts and violations.
12
+
13
+ ## How it Works
14
+
15
+ Leveraging FastMCP's [Middleware][fastmcp-middleware], the Permit.io middleware intercepts all MCP requests to your server and automatically maps MCP methods to authorization checks against your Permit.io policies; covering both server methods and tool execution.
16
+
17
+ ### Policy Mapping
18
+
19
+ The middleware automatically maps MCP methods to Permit.io resources and actions:
20
+
21
+ - **MCP server methods** (e.g., `tools/list`, `resources/read`):
22
+ - **Resource**: `{server_name}_{component}` (e.g., `myserver_tools`)
23
+ - **Action**: The method verb (e.g., `list`, `read`)
24
+ - **Tool execution** (method `tools/call`):
25
+ - **Resource**: `{server_name}` (e.g., `myserver`)
26
+ - **Action**: The tool name (e.g., `greet`)
27
+
28
+ ![Permit.io Policy Mapping Example](./images/policy_mapping.png)
29
+
30
+ *Example: In Permit.io, the 'Admin' role is granted permissions on resources and actions as mapped by the middleware. For example, 'greet', 'greet-jwt', and 'login' are actions on the 'mcp_server' resource, and 'list' is an action on the 'mcp_server_tools' resource.*
31
+
32
+ > **Note:**
33
+ > Don't forget to assign the relevant role (e.g., Admin, User) to the user authenticating to your MCP server (such as the user in the JWT) in the Permit.io Directory. Without the correct role assignment, users will not have access to the resources and actions you've configured in your policies.
34
+ >
35
+ > ![Permit.io Directory Role Assignment Example](./images/role_assignement.png)
36
+ >
37
+ > *Example: In Permit.io Directory, both 'client' and 'admin' users are assigned the 'Admin' role, granting them the permissions defined in your policy mapping.*
38
+
39
+ For detailed policy mapping examples and configuration, see [Detailed Policy Mapping](https://github.com/permitio/permit-fastmcp/blob/main/docs/policy-mapping.md).
40
+
41
+ ### Listing Operations
42
+
43
+ The middleware behaves as a filter for listing operations (`tools/list`, `resources/list`, `prompts/list`), hiding to the client components that are not authorized by the defined policies.
44
+
45
+ ```mermaid
46
+ sequenceDiagram
47
+ participant MCPClient as MCP Client
48
+ participant PermitMiddleware as Permit.io Middleware
49
+ participant MCPServer as FastMCP Server
50
+ participant PermitPDP as Permit.io PDP
51
+
52
+ MCPClient->>PermitMiddleware: MCP Listing Request (e.g., tools/list)
53
+ PermitMiddleware->>MCPServer: MCP Listing Request
54
+ MCPServer-->>PermitMiddleware: MCP Listing Response
55
+ PermitMiddleware->>PermitPDP: Authorization Checks
56
+ PermitPDP->>PermitMiddleware: Authorization Decisions
57
+ PermitMiddleware-->>MCPClient: Filtered MCP Listing Response
58
+ ```
59
+
60
+ ### Execution Operations
61
+
62
+ The middleware behaves as an enforcement point for execution operations (`tools/call`, `resources/read`, `prompts/get`), blocking operations that are not authorized by the defined policies.
63
+
64
+ ```mermaid
65
+ sequenceDiagram
66
+ participant MCPClient as MCP Client
67
+ participant PermitMiddleware as Permit.io Middleware
68
+ participant MCPServer as FastMCP Server
69
+ participant PermitPDP as Permit.io PDP
70
+
71
+ MCPClient->>PermitMiddleware: MCP Execution Request (e.g., tools/call)
72
+ PermitMiddleware->>PermitPDP: Authorization Check
73
+ PermitPDP->>PermitMiddleware: Authorization Decision
74
+ PermitMiddleware-->>MCPClient: MCP Unauthorized Error (if denied)
75
+ PermitMiddleware->>MCPServer: MCP Execution Request (if allowed)
76
+ MCPServer-->>PermitMiddleware: MCP Execution Response (if allowed)
77
+ PermitMiddleware-->>MCPClient: MCP Execution Response (if allowed)
78
+ ```
79
+
80
+ ## Add Authorization to Your Server
81
+
82
+ <Note>
83
+ Permit.io is a cloud-native authorization service. You need a Permit.io account and a running Policy Decision Point (PDP) for the middleware to function. You can run the PDP locally with Docker or use Permit.io's cloud PDP.
84
+ </Note>
85
+
86
+ ### Prerequisites
87
+
88
+ 1. **Permit.io Account**: Sign up at [permit.io](https://permit.io)
89
+ 2. **PDP Setup**: Run the Permit.io PDP locally or use the cloud PDP (RBAC only)
90
+ 3. **API Key**: Get your Permit.io API key from the dashboard
91
+
92
+ ### Run the Permit.io PDP
93
+
94
+ Run the PDP locally with Docker:
95
+
96
+ ```bash
97
+ docker run -p 7766:7766 permitio/pdp:latest
98
+ ```
99
+
100
+ Or use the cloud PDP URL: `https://cloudpdp.api.permit.io`
101
+
102
+ ### Create a Server with Authorization
103
+
104
+ First, install the `permit-fastmcp` package:
105
+
106
+ ```bash
107
+ # Using UV (recommended)
108
+ uv add permit-fastmcp
109
+
110
+ # Using pip
111
+ pip install permit-fastmcp
112
+ ```
113
+
114
+ Then create a FastMCP server and add the Permit.io middleware:
115
+
116
+ ```python server.py
117
+ from fastmcp import FastMCP
118
+ from permit_fastmcp.middleware.middleware import PermitMcpMiddleware
119
+
120
+ mcp = FastMCP("Secure FastMCP Server 🔒")
121
+
122
+ @mcp.tool
123
+ def greet(name: str) -> str:
124
+ """Greet a user by name"""
125
+ return f"Hello, {name}!"
126
+
127
+ @mcp.tool
128
+ def add(a: int, b: int) -> int:
129
+ """Add two numbers"""
130
+ return a + b
131
+
132
+ # Add Permit.io authorization middleware
133
+ mcp.add_middleware(PermitMcpMiddleware(
134
+ permit_pdp_url="http://localhost:7766",
135
+ permit_api_key="your-permit-api-key"
136
+ ))
137
+
138
+ if __name__ == "__main__":
139
+ mcp.run(transport="http")
140
+ ```
141
+
142
+ ### Configure Access Policies
143
+
144
+ Create your authorization policies in the Permit.io dashboard:
145
+
146
+ 1. **Create Resources**: Define resources like `mcp_server` and `mcp_server_tools`
147
+ 2. **Define Actions**: Add actions like `greet`, `add`, `list`, `read`
148
+ 3. **Create Roles**: Define roles like `Admin`, `User`, `Guest`
149
+ 4. **Assign Permissions**: Grant roles access to specific resources and actions
150
+ 5. **Assign Users**: Assign roles to users in the Permit.io Directory
151
+
152
+ For step-by-step setup instructions and troubleshooting, see [Getting Started & FAQ](https://github.com/permitio/permit-fastmcp/blob/main/docs/getting-started.md).
153
+
154
+ #### Example Policy Configuration
155
+
156
+ Policies are defined in the Permit.io dashboard, but you can also use the [Permit.io Terraform provider](https://github.com/permitio/terraform-provider-permitio) to define policies in code.
157
+
158
+
159
+ ```terraform
160
+ # Resources
161
+ resource "permitio_resource" "mcp_server" {
162
+ name = "mcp_server"
163
+ key = "mcp_server"
164
+
165
+ actions = {
166
+ "greet" = { name = "greet" }
167
+ "add" = { name = "add" }
168
+ }
169
+ }
170
+
171
+ resource "permitio_resource" "mcp_server_tools" {
172
+ name = "mcp_server_tools"
173
+ key = "mcp_server_tools"
174
+
175
+ actions = {
176
+ "list" = { name = "list" }
177
+ }
178
+ }
179
+
180
+ # Roles
181
+ resource "permitio_role" "Admin" {
182
+ key = "Admin"
183
+ name = "Admin"
184
+ permissions = [
185
+ "mcp_server:greet",
186
+ "mcp_server:add",
187
+ "mcp_server_tools:list"
188
+ ]
189
+ }
190
+ ```
191
+
192
+ You can also use the [Permit.io CLI](https://github.com/permitio/permit-cli), [API](https://api.permit.io/scalar) or [SDKs](https://github.com/permitio/permit-python) to manage policies, as well as writing policies directly in REGO (Open Policy Agent's policy language).
193
+
194
+ For complete policy examples including ABAC and RBAC configurations, see [Example Policies](https://github.com/permitio/permit-fastmcp/tree/main/docs/example_policies).
195
+
196
+ ### Identity Management
197
+
198
+ The middleware supports multiple identity extraction modes:
199
+
200
+ - **Fixed Identity**: Use a fixed identity for all requests
201
+ - **Header-based**: Extract identity from HTTP headers
202
+ - **JWT-based**: Extract and verify JWT tokens
203
+ - **Source-based**: Use the MCP context source field
204
+
205
+ For detailed identity mode configuration and environment variables, see [Identity Modes & Environment Variables](https://github.com/permitio/permit-fastmcp/blob/main/docs/identity-modes.md).
206
+
207
+ #### JWT Authentication Example
208
+
209
+ ```python
210
+ import os
211
+
212
+ # Configure JWT identity extraction
213
+ os.environ["PERMIT_MCP_IDENTITY_MODE"] = "jwt"
214
+ os.environ["PERMIT_MCP_IDENTITY_JWT_SECRET"] = "your-jwt-secret"
215
+
216
+ mcp.add_middleware(PermitMcpMiddleware(
217
+ permit_pdp_url="http://localhost:7766",
218
+ permit_api_key="your-permit-api-key"
219
+ ))
220
+ ```
221
+
222
+ ### ABAC Policies with Tool Arguments
223
+
224
+ The middleware supports Attribute-Based Access Control (ABAC) policies that can evaluate tool arguments as attributes. Tool arguments are automatically flattened as individual attributes (e.g., `arg_name`, `arg_number`) for granular policy conditions.
225
+
226
+ ![ABAC Condition Example](./images/abac_condition_example.png)
227
+
228
+ *Example: Create dynamic resources with conditions like `resource.arg_number greater-than 10` to allow the `conditional-greet` tool only when the number argument exceeds 10.*
229
+
230
+ #### Example: Conditional Access
231
+
232
+ Create a dynamic resource with conditions like `resource.arg_number greater-than 10` to allow the `conditional-greet` tool only when the number argument exceeds 10.
233
+
234
+ ```python
235
+ @mcp.tool
236
+ def conditional_greet(name: str, number: int) -> str:
237
+ """Greet a user only if number > 10"""
238
+ return f"Hello, {name}! Your number is {number}"
239
+ ```
240
+
241
+ ![ABAC Policy Example](./images/abac_policy_example.png)
242
+
243
+ *Example: The Admin role is granted access to the "conditional-greet" action on the "Big-greets" dynamic resource, while other tools like "greet", "greet-jwt", and "login" are granted on the base "mcp_server" resource.*
244
+
245
+ For comprehensive ABAC configuration and advanced policy examples, see [ABAC Policies with Tool Arguments](https://github.com/permitio/permit-fastmcp/blob/main/docs/policy-mapping.md#abac-policies-with-tool-arguments).
246
+
247
+ ### Run the Server
248
+
249
+ Start your FastMCP server normally:
250
+
251
+ ```bash
252
+ python server.py
253
+ ```
254
+
255
+ The middleware will now intercept all MCP requests and check them against your Permit.io policies. Requests include user identification through the configured identity mode and automatic mapping of MCP methods to authorization resources and actions.
256
+
257
+ ## Advanced Configuration
258
+
259
+ ### Environment Variables
260
+
261
+ Configure the middleware using environment variables:
262
+
263
+ ```bash
264
+ # Permit.io configuration
265
+ export PERMIT_MCP_PERMIT_PDP_URL="http://localhost:7766"
266
+ export PERMIT_MCP_PERMIT_API_KEY="your-api-key"
267
+
268
+ # Identity configuration
269
+ export PERMIT_MCP_IDENTITY_MODE="jwt"
270
+ export PERMIT_MCP_IDENTITY_JWT_SECRET="your-jwt-secret"
271
+
272
+ # Method configuration
273
+ export PERMIT_MCP_KNOWN_METHODS='["tools/list","tools/call"]'
274
+ export PERMIT_MCP_BYPASSED_METHODS='["initialize","ping"]'
275
+
276
+ # Logging configuration
277
+ export PERMIT_MCP_ENABLE_AUDIT_LOGGING="true"
278
+ ```
279
+
280
+ For a complete list of all configuration options and environment variables, see [Configuration Reference](https://github.com/permitio/permit-fastmcp/blob/main/docs/configuration-reference.md).
281
+
282
+ ### Custom Middleware Configuration
283
+
284
+ ```python
285
+ from permit_fastmcp.middleware.middleware import PermitMcpMiddleware
286
+
287
+ middleware = PermitMcpMiddleware(
288
+ permit_pdp_url="http://localhost:7766",
289
+ permit_api_key="your-api-key",
290
+ enable_audit_logging=True,
291
+ bypass_methods=["initialize", "ping", "health/*"]
292
+ )
293
+
294
+ mcp.add_middleware(middleware)
295
+ ```
296
+
297
+ For advanced configuration options and custom middleware extensions, see [Advanced Configuration](https://github.com/permitio/permit-fastmcp/blob/main/docs/advanced-configuration.md).
298
+
299
+ ## Example: Complete JWT Authentication Server
300
+
301
+ See the [example server](https://github.com/permitio/permit-fastmcp/blob/main/permit_fastmcp/example_server/example.py) for a full implementation with JWT-based authentication. For additional examples and usage patterns, see [Example Server](https://github.com/permitio/permit-fastmcp/blob/main/permit_fastmcp/example_server/):
302
+
303
+ ```python
304
+ from fastmcp import FastMCP, Context
305
+ from permit_fastmcp.middleware.middleware import PermitMcpMiddleware
306
+ import jwt
307
+ import datetime
308
+
309
+ # Configure JWT identity extraction
310
+ os.environ["PERMIT_MCP_IDENTITY_MODE"] = "jwt"
311
+ os.environ["PERMIT_MCP_IDENTITY_JWT_SECRET"] = "mysecretkey"
312
+
313
+ mcp = FastMCP("My MCP Server")
314
+
315
+ @mcp.tool
316
+ def login(username: str, password: str) -> str:
317
+ """Login to get a JWT token"""
318
+ if username == "admin" and password == "password":
319
+ token = jwt.encode(
320
+ {"sub": username, "exp": datetime.datetime.utcnow() + datetime.timedelta(hours=1)},
321
+ "mysecretkey",
322
+ algorithm="HS256"
323
+ )
324
+ return f"Bearer {token}"
325
+ raise Exception("Invalid credentials")
326
+
327
+ @mcp.tool
328
+ def greet_jwt(ctx: Context) -> str:
329
+ """Greet a user by extracting their name from JWT"""
330
+ # JWT extraction handled by middleware
331
+ return "Hello, authenticated user!"
332
+
333
+ mcp.add_middleware(PermitMcpMiddleware(
334
+ permit_pdp_url="http://localhost:7766",
335
+ permit_api_key="your-permit-api-key"
336
+ ))
337
+
338
+ if __name__ == "__main__":
339
+ mcp.run(transport="http")
340
+ ```
341
+
342
+ <Tip>
343
+ For detailed policy configuration, custom authentication, and advanced
344
+ deployment patterns, visit the [Permit.io FastMCP Middleware
345
+ repository][permit-fastmcp-github]. For troubleshooting common issues, see [Troubleshooting](https://github.com/permitio/permit-fastmcp/blob/main/docs/troubleshooting.md).
346
+ </Tip>
347
+
348
+
349
+ [permit.io]: https://www.permit.io
350
+ [permit-github]: https://github.com/permitio
351
+ [permit-fastmcp-github]: https://github.com/permitio/permit-fastmcp
352
+ [Agent.Security]: https://agent.security
353
+ [fastmcp-middleware]: /servers/middleware