UNPKG

@openai/agents-core

Version:

The OpenAI Agents SDK is a lightweight yet powerful framework for building multi-agent workflows.

148 lines • 5.39 kB
import debug from 'debug'; import { z } from 'zod'; import { getLogger } from "./logger.mjs"; export const DEFAULT_STDIO_MCP_CLIENT_LOGGER_NAME = 'openai-agents:stdio-mcp-client'; export const DEFAULT_STREAMABLE_HTTP_MCP_CLIENT_LOGGER_NAME = 'openai-agents:streamable-http-mcp-client'; export const DEFAULT_SSE_MCP_CLIENT_LOGGER_NAME = 'openai-agents:sse-mcp-client'; export class BaseMCPServerStdio { cacheToolsList; _cachedTools = undefined; toolFilter; toolMetaResolver; customDataExtractor; toolInputGuardrails; toolOutputGuardrails; useStructuredContent; errorFunction; logger; constructor(options) { this.logger = options.logger ?? getLogger(DEFAULT_STDIO_MCP_CLIENT_LOGGER_NAME); this.cacheToolsList = options.cacheToolsList ?? false; this.toolFilter = options.toolFilter; this.toolMetaResolver = options.toolMetaResolver; this.customDataExtractor = options.customDataExtractor; this.toolInputGuardrails = options.toolInputGuardrails; this.toolOutputGuardrails = options.toolOutputGuardrails; this.useStructuredContent = options.useStructuredContent; this.errorFunction = options.errorFunction; } /** * Logs a debug message when debug logging is enabled. * @param buildMessage A function that returns the message to log. */ debugLog(buildMessage) { if (!this.logger.dontLogToolData && debug.enabled(this.logger.namespace)) { // Only build the message when debug logging is enabled. this.logger.debug(buildMessage()); } } } export class BaseMCPServerStreamableHttp { cacheToolsList; _cachedTools = undefined; toolFilter; toolMetaResolver; customDataExtractor; toolInputGuardrails; toolOutputGuardrails; useStructuredContent; errorFunction; logger; constructor(options) { this.logger = options.logger ?? getLogger(DEFAULT_STREAMABLE_HTTP_MCP_CLIENT_LOGGER_NAME); this.cacheToolsList = options.cacheToolsList ?? false; this.toolFilter = options.toolFilter; this.toolMetaResolver = options.toolMetaResolver; this.customDataExtractor = options.customDataExtractor; this.toolInputGuardrails = options.toolInputGuardrails; this.toolOutputGuardrails = options.toolOutputGuardrails; this.useStructuredContent = options.useStructuredContent; this.errorFunction = options.errorFunction; } /** * Logs a debug message when debug logging is enabled. * @param buildMessage A function that returns the message to log. */ debugLog(buildMessage) { if (!this.logger.dontLogToolData && debug.enabled(this.logger.namespace)) { // Only build the message when debug logging is enabled. this.logger.debug(buildMessage()); } } } export class BaseMCPServerSSE { cacheToolsList; _cachedTools = undefined; toolFilter; toolMetaResolver; customDataExtractor; toolInputGuardrails; toolOutputGuardrails; useStructuredContent; errorFunction; logger; constructor(options) { this.logger = options.logger ?? getLogger(DEFAULT_SSE_MCP_CLIENT_LOGGER_NAME); this.cacheToolsList = options.cacheToolsList ?? false; this.toolFilter = options.toolFilter; this.toolMetaResolver = options.toolMetaResolver; this.customDataExtractor = options.customDataExtractor; this.toolInputGuardrails = options.toolInputGuardrails; this.toolOutputGuardrails = options.toolOutputGuardrails; this.useStructuredContent = options.useStructuredContent; this.errorFunction = options.errorFunction; } /** * Logs a debug message when debug logging is enabled. * @param buildMessage A function that returns the message to log. */ debugLog(buildMessage) { if (!this.logger.dontLogToolData && debug.enabled(this.logger.namespace)) { // Only build the message when debug logging is enabled. this.logger.debug(buildMessage()); } } } /** * Minimum MCP tool data definition. * This type definition does not intend to cover all possible properties. * It supports the properties that are used in this SDK. */ export const MCPTool = z.object({ name: z.string(), description: z.string().optional(), annotations: z .looseObject({ title: z.string().optional(), readOnlyHint: z.boolean().optional(), destructiveHint: z.boolean().optional(), idempotentHint: z.boolean().optional(), openWorldHint: z.boolean().optional(), }) .optional(), inputSchema: z.object({ type: z.literal('object'), properties: z.record(z.string(), z.any()), required: z.array(z.string()), additionalProperties: z.boolean(), }), }); export function attachCallToolResultMetadata(content, metadata) { const result = content; for (const [key, value] of Object.entries(metadata)) { if (typeof value === 'undefined') { continue; } Object.defineProperty(result, key, { value, enumerable: false, configurable: true, }); } return result; } //# sourceMappingURL=mcpShared.mjs.map