@toolplex/client
Version:
The official ToolPlex client for AI agent tool discovery and execution
106 lines (105 loc) • 3.7 kB
JavaScript
import { PlaybookPolicy } from "./playbookPolicy.js";
import { FeedbackPolicy } from "./feedbackPolicy.js";
import CallToolObserver from "./callToolObserver.js";
import InstallObserver from "./installObserver.js";
import { ServerPolicy } from "./serverPolicy.js";
export class PolicyEnforcer {
constructor() {
this.playbookPolicy = null;
this.feedbackPolicy = null;
this.serverPolicy = null;
this.callToolObserver = null;
this.installObserver = null;
}
/**
* Initialize the policy enforcer with the client context
*/
init(clientContext) {
this.callToolObserver = new CallToolObserver();
this.installObserver = new InstallObserver();
this.playbookPolicy = new PlaybookPolicy(clientContext, this.callToolObserver);
this.feedbackPolicy = new FeedbackPolicy(clientContext, this.callToolObserver, this.installObserver);
this.serverPolicy = new ServerPolicy(clientContext);
}
/**
* Enforce playbook policy validation before saving.
* Throws if the playbook does not pass policy.
*/
enforceSavePlaybookPolicy(playbook) {
if (!this.playbookPolicy) {
throw new Error("PolicyEnforcer not initialized");
}
this.playbookPolicy.enforceSavePlaybookPolicy(playbook);
}
/**
* Enforce feedback policy validation.
* Throws if the feedback does not pass policy.
*/
enforceFeedbackPolicy(feedback) {
if (!this.feedbackPolicy) {
throw new Error("PolicyEnforcer not initialized");
}
this.feedbackPolicy.enforceFeedbackPolicy(feedback);
}
/**
* Enforce server call tool policy validation.
* Throws if attempting to call a tool on a blocked server.
*/
enforceCallToolPolicy(serverId) {
if (!this.serverPolicy) {
throw new Error("PolicyEnforcer not initialized");
}
this.serverPolicy.enforceCallToolPolicy(serverId);
}
/**
* Enforce server config policy validation.
* Throws if attempting to use a blocked or disallowed server.
*/
enforceUseServerPolicy(serverId) {
if (!this.serverPolicy) {
throw new Error("PolicyEnforcer not initialized");
}
this.serverPolicy.enforceUseServerPolicy(serverId);
}
/**
* Enforce playbook usage logging policy validation.
* Throws if read-only mode is enabled.
*/
enforceLogPlaybookUsagePolicy() {
if (!this.playbookPolicy) {
throw new Error("PolicyEnforcer not initialized");
}
this.playbookPolicy.enforceLogPlaybookUsagePolicy();
}
/**
* Filters out blocked servers from a list of objects.
*
* @param servers List of objects containing server IDs
* @param getServerId Function that extracts the server ID from an object
* @returns Filtered list with blocked servers removed
*/
filterBlockedMcpServers(servers, getServerId) {
if (!this.serverPolicy) {
throw new Error("PolicyEnforcer not initialized");
}
return this.serverPolicy.filterBlockedMcpServers(servers, getServerId);
}
/**
* Get a reference to the CallToolObserver instance.
*/
getCallToolObserver() {
if (!this.callToolObserver) {
throw new Error("PolicyEnforcer not initialized");
}
return this.callToolObserver;
}
/**
* Get a reference to the InstallObserver instance.
*/
getInstallObserver() {
if (!this.installObserver) {
throw new Error("PolicyEnforcer not initialized");
}
return this.installObserver;
}
}