-
Notifications
You must be signed in to change notification settings - Fork 82
Filter MCP features based on granular RBACs #6494
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
+493
−26
Merged
Changes from all commits
Commits
Show all changes
15 commits
Select commit
Hold shift + click to select a range
1c3e8f7
Add MCP RBAC permissions for expert insights tools
Steve-Mcl 31d09be
Add MCP RBAC permissions filter function
Steve-Mcl 69619d4
Include instance requested state in the associated models of MCP Regi…
Steve-Mcl 353a521
Add teamMembership to request object for later use
Steve-Mcl 93f0fd9
Skip instances not required to be 'running' - avoid unnecessary timeouts
Steve-Mcl 22f1ea8
Filter MCP servers and features based on RBACS
Steve-Mcl 8afb010
Filter MCP capabilities based on user access in expert API
Steve-Mcl bfb5d23
Dont show tools/resources/prompts header if none are present
Steve-Mcl 5f8f184
fix existing tests (addition of application prop)
Steve-Mcl 6f79944
Add granular RBAC test for MCP features based on user permissions
Steve-Mcl 307725c
Merge branch 'main' into 6491-mcp-rbacs
Steve-Mcl db8c266
Update forge/services/expert.js
Steve-Mcl cb9bd2c
Merge branch 'main' into 6491-mcp-rbacs
Steve-Mcl 9afc675
Merge branch 'main' into 6491-mcp-rbacs
Steve-Mcl 1943d77
Merge branch 'main' into 6491-mcp-rbacs
Steve-Mcl File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,109 @@ | ||
| /** | ||
| * Filter MCP server features based on user access permissions for the owner application. | ||
| * If a user does not have access to a specific feature (e.g. a tool with destructive hint), it is removed from the server's feature list. | ||
| * If a server has no accessible features after filtering, it is removed from the list. | ||
| * @param {ForgeApplication} app | ||
| * @param {Array<{server: object, application: import('sequelize').Model, applicationId: string, instanceId: string}>} serverList | ||
| * @param {import('sequelize').Model} team | ||
| * @param {import('sequelize').Model} teamMembership | ||
| * @returns {MCPServerItem[]} | ||
| */ | ||
| module.exports.filterAccessibleMCPServerFeatures = function (app, serverList, team, teamMembership) { | ||
| const servers = [] | ||
| for (const serverDetails of serverList) { | ||
| const { server, application } = serverDetails | ||
| const permissionContext = { application } | ||
|
|
||
| // sanity checks | ||
| if (!application) { | ||
| continue // not expected, but just in case | ||
| } | ||
| if (team.id !== application.TeamId || team.hashid !== server.team) { | ||
| continue | ||
| } | ||
|
|
||
| // first pass - is the user allowed access to this MCP server at all? | ||
| if (!app.hasPermission(teamMembership, 'expert:insights:mcp:allow', { application })) { | ||
| continue // user doesn't have access to this instance | ||
| } | ||
|
|
||
| // NOTE: prompts are not yet implemented in the expert backend | ||
| // const defaultPromptPermission = app.hasPermission(teamMembership, 'expert:insights:mcp:prompt:allow', permissionContext) | ||
| const defaultResourcePermission = app.hasPermission(teamMembership, 'expert:insights:mcp:resource:allow', permissionContext) | ||
| const defaultResourceTemplatePermission = app.hasPermission(teamMembership, 'expert:insights:mcp:resourcetemplate:allow', permissionContext) | ||
| const defaultToolPermission = app.hasPermission(teamMembership, 'expert:insights:mcp:tool:allow', permissionContext) | ||
| const allowToolWrite = app.hasPermission(teamMembership, 'expert:insights:mcp:tool:write', permissionContext) | ||
| const allowToolDestructive = app.hasPermission(teamMembership, 'expert:insights:mcp:tool:destructive', permissionContext) | ||
| const allowToolOpenWorld = app.hasPermission(teamMembership, 'expert:insights:mcp:tool:open-world', permissionContext) | ||
| const allowToolNonIdempotent = app.hasPermission(teamMembership, 'expert:insights:mcp:tool:non-idempotent', permissionContext) | ||
|
|
||
| const result = { ...server } | ||
|
|
||
| if (result.resources && Array.isArray(result.resources)) { | ||
| result.resources = result.resources.filter(_resource => { | ||
| return defaultResourcePermission | ||
| }) | ||
| } | ||
|
|
||
| if (result.resourceTemplates && Array.isArray(result.resourceTemplates)) { | ||
| result.resourceTemplates = result.resourceTemplates.filter(_resourceTemplate => { | ||
| return defaultResourceTemplatePermission | ||
| }) | ||
| } | ||
|
|
||
| if (result.tools && Array.isArray(result.tools)) { | ||
| result.tools = result.tools.filter(tool => { | ||
| if (defaultToolPermission !== true) { | ||
| return false | ||
| } | ||
| // at this point, we have established the user has general tool access - now filter based on annotations/hints | ||
| const isReadonly = tool.annotations?.readOnlyHint === true | ||
| const isDestructive = tool.annotations?.destructiveHint !== false // air on side of caution - if not specified, assume destructive | ||
| const isOpenWorld = tool.annotations?.openWorldHint === true | ||
| const isIdempotent = tool.annotations?.idempotentHint === true | ||
| const writeAccessRequired = isDestructive === true || isReadonly === false | ||
|
|
||
| // Sanity check combinations | ||
| if (isReadonly && isDestructive) { | ||
| // this is not a valid combination - destructive tools cannot be read-only | ||
| return false | ||
| } | ||
|
|
||
| // test access based on hints - worst to best | ||
| if (writeAccessRequired) { | ||
| if (isDestructive === true) { | ||
| if (!allowToolDestructive) { | ||
| return false | ||
| } | ||
| } | ||
| if (isReadonly === false) { | ||
| if (!allowToolWrite) { | ||
| return false | ||
| } | ||
| } | ||
| if (isIdempotent === false) { | ||
| if (!allowToolNonIdempotent) { | ||
| return false | ||
| } | ||
| } | ||
| } | ||
| if (isOpenWorld === true) { | ||
| if (!allowToolOpenWorld) { | ||
| return false | ||
| } | ||
| } | ||
| return true | ||
| }) | ||
| } | ||
| servers.push(result) | ||
| } | ||
|
|
||
| // finally, before sending the response, filter out any servers that have no accessible features | ||
| return servers.filter(server => { | ||
| const hasPrompts = Array.isArray(server.prompts) && server.prompts.length > 0 | ||
| const hasResources = Array.isArray(server.resources) && server.resources.length > 0 | ||
| const hasResourceTemplates = Array.isArray(server.resourceTemplates) && server.resourceTemplates.length > 0 | ||
| const hasTools = Array.isArray(server.tools) && server.tools.length > 0 | ||
| return hasPrompts || hasResources || hasResourceTemplates || hasTools | ||
| }) | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.