@general-analysis/mcp-guard
Version:
An MCP guardrail with built-in AI-powered moderation that aggregates multiple MCP servers into one secure interface
61 lines • 3.01 kB
JavaScript
import { BLOCKED_RESPONSE, createPaymentRequiredResponse } from "./types.js";
// API Configuration
const API_URL = "https://api.generalanalysis.com";
/**
* Moderates tool output using the Guard API to detect prompt injection attacks
* @param output - The tool output to moderate
* @param apiKey - The API key for the Guard API
* @param enableGuardApi - Whether the Guard API is enabled
* @returns The original output or a blocked response if harmful content is detected
*/
export async function moderateToolOutput(output, apiKey, enableGuardApi = false) {
if (!output || !output.content || !Array.isArray(output.content)) {
return output;
}
// Extract text content for moderation
for (const contentItem of output.content) {
if (contentItem.type === "text" && typeof contentItem.text === "string") {
const text = contentItem.text;
// Call guard API only if enabled and API key is provided
if (enableGuardApi && apiKey) {
try {
const controller = new AbortController();
const timeoutId = setTimeout(() => controller.abort(), 5000); // 5 second timeout
const response = await fetch(API_URL + "/guard", {
method: "POST",
headers: {
Authorization: `Bearer ${apiKey}`,
"Content-Type": "application/json",
},
body: JSON.stringify({
text: text,
policy_name: "@ga/mcp-injection",
}),
signal: controller.signal,
});
clearTimeout(timeoutId);
if (!response.ok) {
// Handle 402 Payment Required specifically
if (response.status === 402) {
// Return original output with payment warning
return createPaymentRequiredResponse(output);
}
throw new Error(`Guard API returned error: ${response.status} ${response.statusText}`);
}
const guardResult = (await response.json());
// Check if either heuristic or injection guard flagged the content
const heuristicFlagged = guardResult.injection_heuristic?.flagged || false;
const injectionGuardFlagged = guardResult.injection_guard?.flagged || false;
if (heuristicFlagged || injectionGuardFlagged) {
return BLOCKED_RESPONSE;
}
}
catch (error) {
throw new Error(`Guard API call failed: ${error instanceof Error ? error.message : String(error)}`);
}
}
}
}
return output;
}
//# sourceMappingURL=moderation.js.map