-
Notifications
You must be signed in to change notification settings - Fork 0
Add user-defined policies #2
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
Draft
bhelx
wants to merge
1
commit into
main
Choose a base branch
from
user-defined-policies
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from all commits
Commits
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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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 |
---|---|---|
@@ -1,2 +1,7 @@ | ||
import { McpxOpenAI } from './openai'; | ||
import { CallToolRequest, CallToolResult } from "@modelcontextprotocol/sdk/types"; | ||
import type { PolicyContext, PolicyFunction } from "./policy-enforcer"; | ||
|
||
export { McpxOpenAI } | ||
export type { CallToolRequest, CallToolResult, PolicyFunction, PolicyContext} | ||
|
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,74 @@ | ||
import { CallToolRequest, CallToolResult } from "@modelcontextprotocol/sdk/types"; | ||
|
||
interface PolicyResult { | ||
allowed: boolean; | ||
reason?: string; | ||
} | ||
|
||
// A policy can inspect both the call and the result | ||
export type PolicyFunction = ( | ||
call: CallToolRequest, | ||
context: PolicyContext, | ||
result?: CallToolResult, | ||
) => Promise<PolicyResult>; | ||
|
||
// Shared context that policies can use to track state | ||
export class PolicyContext { | ||
private state = new Map<string, any>(); | ||
|
||
get(key: string): any { | ||
return this.state.get(key); | ||
} | ||
|
||
set(key: string, value: any): void { | ||
this.state.set(key, value); | ||
} | ||
} | ||
|
||
export class PolicyEnforcer { | ||
private beforePolicies: Map<string, PolicyFunction[]> = new Map(); | ||
private afterPolicies: Map<string, PolicyFunction[]> = new Map(); | ||
private context = new PolicyContext(); | ||
|
||
// Add a policy to run before the function call | ||
addBeforePolicy(functionName: string, policy: PolicyFunction) { | ||
const policies = this.beforePolicies.get(functionName) || []; | ||
policies.push(policy); | ||
this.beforePolicies.set(functionName, policies); | ||
} | ||
|
||
// Add a policy to run after the function call | ||
addAfterPolicy(functionName: string, policy: PolicyFunction) { | ||
const policies = this.afterPolicies.get(functionName) || []; | ||
policies.push(policy); | ||
this.afterPolicies.set(functionName, policies); | ||
} | ||
|
||
async wrapCall( | ||
call: CallToolRequest, | ||
executor: (call: CallToolRequest) => Promise<CallToolResult> | ||
): Promise<any> { | ||
// NOTE: hack to normalized name | ||
const normalizedName = call.params.name.replace(/^.*?_/, '') | ||
const beforePolicies = this.beforePolicies.get(normalizedName) || []; | ||
for (const policy of beforePolicies) { | ||
const result = await policy(call, this.context); | ||
if (!result.allowed) { | ||
throw new Error(`Policy violation: ${result.reason}`); | ||
} | ||
} | ||
|
||
// execute the tool call | ||
const result = await executor(call); | ||
|
||
const afterPolicies = this.afterPolicies.get(normalizedName) || []; | ||
for (const policy of afterPolicies) { | ||
const policyResult = await policy(call, this.context, result); | ||
if (!policyResult.allowed) { | ||
throw new Error(`Policy violation: ${policyResult.reason}`); | ||
} | ||
} | ||
|
||
return result; | ||
} | ||
} |
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
Empty file.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What do you think about folding this into a single
intercept
"middleware" function?I.e., instead of an allow/deny response, give the library the ability to control handling the incoming tool call request or delegating it to the next layer down. This makes it possible to stub tool invocations during tests, call a tool multiple times, return its own response in special circumstances, etc. (You could implement a policy API on top of this.)