Skip to content
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

Add user-defined policies #2

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft

Add user-defined policies #2

wants to merge 1 commit into from

Conversation

bhelx
Copy link
Contributor

@bhelx bhelx commented Jan 8, 2025

This is an experiment and meant to be discussed in the broader context of mcpx and the topic of agent control. The code is fine but is meant to be thrown away for something broader. Please ignore for now.

console.log({hostname, counts, count})

return { allowed: true }
})

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?

mcpx.intercept("fetch", async (request, context, next) => {
  const { hostname } = new URL(request.params.arguments.url)
  const counts = context.get("fetch.domain-counts") || {}
  if (!context.has("fetch.domain-counts")) {
    context.set("fetch.domain-counts", counts)
  }

  counts[hostname] ??= 0
  // we can only call each domain 3 times for some reason!
  // our lawyers are making us!
  if (counts[hostname] >= 3) {
    console.log(`failed`)
    return {
      isError: true,
      text: `The domain ${hostname} was called too many times: ${currentCount}`
    }
  }

  const result = await next(request, context)

  ++counts[hostname]
  console.log({hostname, counts, count})

  return result
})

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.)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants