UNPKG

@mastra/core

Version:

Mastra is the Typescript framework for building AI agents and assistants. It’s used by some of the largest companies in the world to build internal AI automation tooling and customer-facing agents.

158 lines 7.05 kB
import type { ToolsInput } from '../agent/index.js'; import { MastraBase } from '../base.js'; import type { Mastra } from '../mastra/index.js'; import type { ConvertedTool, MCPServerConfig, MCPServerHonoSSEOptions, MCPServerHTTPOptions, MCPServerSSEOptions, MCPToolType, PackageInfo, RemoteInfo, Repository, ServerDetailInfo, ServerInfo } from './types.js'; export * from './types.js'; /** * Abstract base class for MCP server implementations. * This provides a common interface and shared functionality for all MCP servers * that can be registered with Mastra, including handling of server metadata. */ export declare abstract class MCPServerBase extends MastraBase { /** Tracks if the server ID has been definitively set. */ private idWasSet; /** The display name of the MCP server. */ readonly name: string; /** The semantic version of the MCP server. */ readonly version: string; /** Internal storage for the server's unique ID. */ private _id; /** A description of what the MCP server does. */ readonly description?: string; /** Repository information for the server's source code. */ readonly repository?: Repository; /** The release date of this server version (ISO 8601 string). */ readonly releaseDate: string; /** Indicates if this version is the latest available. */ readonly isLatest: boolean; /** The canonical packaging format (e.g., "npm", "docker"), if applicable. */ readonly packageCanonical?: MCPServerConfig['packageCanonical']; /** Information about installable packages for this server. */ readonly packages?: PackageInfo[]; /** Information about remote access points for this server. */ readonly remotes?: RemoteInfo[]; /** The tools registered with and converted by this MCP server. */ convertedTools: Record<string, ConvertedTool>; /** Reference to the Mastra instance if this server is registered with one. */ mastra: Mastra | undefined; /** Agents to be exposed as tools. */ protected readonly agents?: MCPServerConfig['agents']; /** Workflows to be exposed as tools. */ protected readonly workflows?: MCPServerConfig['workflows']; /** Original tools configuration for re-conversion when Mastra instance is registered. */ protected readonly originalTools: ToolsInput; /** * Public getter for the server's unique ID. * The ID is set at construction or by Mastra and is read-only afterwards. */ get id(): string; /** * Gets a read-only view of the registered tools. * @returns A readonly record of converted tools. */ tools(): Readonly<Record<string, ConvertedTool>>; /** * Sets the server's unique ID. This method is typically called by Mastra when * registering the server, using the key provided in the Mastra configuration. * It ensures the ID is set only once. * If an ID was already provided in the MCPServerConfig, this method will be a no-op. * @param id The unique ID to assign to the server. */ setId(id: string): void; /** * Abstract method to convert and validate tool definitions provided to the server. * This method will also handle agents passed in the config. * @param tools Tool definitions to convert. * @param agents Agent definitions to convert to tools. * @param workflows Workflow definitions to convert to tools. * @returns A record of converted and validated tools. */ abstract convertTools(tools: ToolsInput, agents?: MCPServerConfig['agents'], workflows?: MCPServerConfig['workflows']): Record<string, ConvertedTool>; /** * Internal method used by Mastra to register itself with the server. * @param mastra The Mastra instance. * @internal */ __registerMastra(mastra: Mastra): void; /** * Constructor for the MCPServerBase. * @param config Configuration options for the MCP server, including metadata. */ constructor(config: MCPServerConfig); /** * Start the MCP server using stdio transport * This is typically used for Windsurf integration */ abstract startStdio(): Promise<void>; /** * Start the MCP server using SSE transport * This is typically used for web integration * @param options Options for the SSE transport */ abstract startSSE(options: MCPServerSSEOptions): Promise<void>; /** * Start the MCP server using Hono SSE transport * Used for Hono servers * @param options Options for the SSE transport */ abstract startHonoSSE(options: MCPServerHonoSSEOptions): Promise<Response | undefined>; /** * Start the MCP server using HTTP transport * @param options Options for the HTTP transport */ abstract startHTTP(options: MCPServerHTTPOptions): Promise<void>; /** * Close the MCP server and all its connections */ abstract close(): Promise<void>; /** * Gets the basic information about the server, conforming to the MCP Registry 'Server' schema. * This information is suitable for listing multiple servers. * @returns ServerInfo object containing basic server metadata. */ abstract getServerInfo(): ServerInfo; /** * Gets detailed information about the server, conforming to the MCP Registry 'ServerDetail' schema. * This includes all information from `getServerInfo` plus package and remote details. * @returns ServerDetailInfo object containing comprehensive server metadata. */ abstract getServerDetail(): ServerDetailInfo; /** * Gets a list of tools provided by this MCP server, including their schemas. * @returns An object containing an array of tool information. */ abstract getToolListInfo(): { tools: Array<{ name: string; description?: string; inputSchema: any; outputSchema?: any; toolType?: MCPToolType; }>; }; /** * Gets information for a specific tool provided by this MCP server. * @param toolId The ID/name of the tool to retrieve. * @returns Tool information (name, description, inputSchema) or undefined if not found. */ abstract getToolInfo(toolId: string): { name: string; description?: string; inputSchema: any; outputSchema?: any; toolType?: MCPToolType; } | undefined; /** * Executes a specific tool provided by this MCP server. * @param toolId The ID/name of the tool to execute. * @param args The arguments to pass to the tool's execute function. * @param executionContext Optional context for the tool execution (e.g., messages, toolCallId). * @returns A promise that resolves to the result of the tool execution. * @throws Error if the tool is not found, or if execution fails. */ abstract executeTool(toolId: string, args: any, executionContext?: { messages?: any[]; toolCallId?: string; }): Promise<any>; } //# sourceMappingURL=index.d.ts.map