ts/docs/api/mcp.old.md
The MCP class provides methods to manage Model Control Protocol servers, enabling AI models to interact with external tools and services through a standardized protocol. MCP servers act as bridges between AI agents and various toolkits, providing secure and managed access to external capabilities.
MCP (Model Control Protocol) is a protocol for exposing tools and capabilities to AI models in a standardized way. With Composio's MCP integration, you can:
Creates a new MCP server with specified toolkit configurations.
// Create an MCP server with Gmail toolkit
const server = await composio.mcp.create(
"my-gmail-server",
[
{
toolkit: "gmail",
authConfigId: "ac_sdhkjfhjksdk",
allowedTools: ["GMAIL_FETCH_EMAILS", "GMAIL_SEND_EMAIL"]
}
],
{ isChatAuth: true }
);
// Create an MCP server with multiple toolkits
const multiToolServer = await composio.mcp.create(
"multi-tool-server",
[
{
toolkit: "github",
authConfigId: "ac_github123",
allowedTools: ["GITHUB_CREATE_ISSUE", "GITHUB_GET_REPO"]
},
{
toolkit: "slack",
authConfigId: "ac_slack456",
allowedTools: ["SLACK_SEND_MESSAGE"]
}
]
);
// Use the convenience method to get server URLs
const urls = await server.getServer({
connectedAccountIds: { gmail: "connected_account_id" }
});
Parameters:
name (string): Unique name for the MCP servertoolkitConfigs (MCPToolkitConfig[]): Array of toolkit configurations
toolkit: The toolkit slug (e.g., "gmail", "github")authConfigId: The auth configuration ID to useallowedTools: Array of specific tool IDs to exposeauthOptions (MCPAuthOptions): Optional authentication options
isChatAuth: Whether to use Composio-managed authenticationReturns: Promise<McpServerCreateResponse> - Created server details with convenience getServer method
Throws: ValidationError if configurations are invalid or server creation fails
Retrieves server URLs for an existing MCP server. This method generates secure URLs that AI agents can use to connect to the MCP server.
// Get URLs for a server using connected account IDs
const urls = await composio.mcp.getServer("server-uuid", {
connectedAccountIds: {
gmail: "connected_account_123",
github: "connected_account_456"
}
});
// Get URLs for a server using user ID
const userUrls = await composio.mcp.getServer("server-uuid", {
userId: "user_123"
});
// Override authentication options
const customAuthUrls = await composio.mcp.getServer(
"server-uuid",
{ userId: "user_123" },
{ isChatAuth: false }
);
Parameters:
id (string): Server UUIDparams (MCPGetServerParams): Parameters for getting server URLs
connectedAccountIds (object mapping toolkit names to account IDs) OR userId (string)authOptions (MCPAuthOptions): Optional authentication options to override server defaultsReturns: Promise<T> - Transformed server URLs in provider-specific format
Throws: ValidationError if parameters are invalid or URL generation fails
Lists MCP server configurations with filtering options.
// List all servers with pagination
const servers = await composio.mcp.list({
page: 1,
limit: 10
});
// List servers for specific toolkits
const gmailServers = await composio.mcp.list({
toolkits: ['gmail', 'google-calendar'],
limit: 20
});
// List servers by auth config
const authFilteredServers = await composio.mcp.list({
authConfigs: ['auth_123', 'auth_456'],
name: 'production'
});
Parameters:
options (object): Filtering and pagination options
page: Page number for pagination (default: 1)limit: Number of items per page (default: 10)toolkits: Filter by toolkit namesauthConfigs: Filter by auth config IDsname: Filter by server nameReturns: Promise<McpListResponse> - List of MCP servers
Retrieves details of a specific MCP server.
const serverDetails = await composio.mcp.get('server-uuid');
console.log(serverDetails.name);
console.log(serverDetails.toolkits);
console.log(serverDetails.status);
Parameters:
id (string): Server UUIDReturns: Promise<McpRetrieveResponse> - Server details including configuration and metadata
Updates an MCP server configuration.
const updatedServer = await composio.mcp.update(
"server-uuid",
"updated-server-name",
[
{
toolkit: "gmail",
authConfigId: "ac_new_auth_config",
allowedTools: ["GMAIL_FETCH_EMAILS", "GMAIL_SEND_EMAIL", "GMAIL_CREATE_DRAFT"]
}
],
{ isChatAtuh: true }
);
Parameters:
id (string): Server UUIDname (string): New name for the servertoolkitConfigs (MCPToolkitConfig[]): Updated toolkit configurationsauthOptions (MCPAuthOptions): Updated authentication optionsReturns: Promise<McpUpdateResponse> - Updated server details
Deletes an MCP server.
const result = await composio.mcp.delete('server-uuid');
console.log(result.deleted); // true
Parameters:
id (string): Server UUIDReturns: Promise<McpDeleteResponse> - Deletion confirmation
Generates URLs for MCP server access. This is a lower-level method primarily used internally.
const urlResponse = await composio.mcp.generateUrl({
userIds: ['user123'],
connectedAccountIds: ['account456'],
mcpServerId: 'server-uuid',
managedAuthByComposio: true
});
Parameters:
params (GenerateURLParams): URL generation parameters
userIds: Array of user IDsconnectedAccountIds: Array of connected account IDsmcpServerId: MCP server IDmanagedAuthByComposio: Whether to use Composio-managed authReturns: Promise<GenerateURLResponse> - Generated URLs
Different AI frameworks expect MCP server URLs in different formats. Composio automatically handles these transformations through provider-specific implementations.
import { Composio } from '@composio/core';
import { MastraProvider } from '@composio/mastra';
import { AnthropicProvider } from '@composio/anthropic';
// With Mastra provider - returns key-value URL mapping
const composioMastra = new Composio({
apiKey: process.env.COMPOSIO_API_KEY,
provider: new MastraProvider()
});
const mastraServer = await composioMastra.mcp.create(...);
const mastraUrls = await mastraServer.getServer({
connectedAccountIds: { gmail: "account_id" }
});
// Returns: { "server-0": { url: "..." } }
// With Anthropic provider - returns array of server info
const composioAnthropic = new Composio({
apiKey: process.env.COMPOSIO_API_KEY,
provider: new AnthropicProvider()
});
const anthropicServer = await composioAnthropic.mcp.create(...);
const anthropicUrls = await anthropicServer.getServer({
connectedAccountIds: { gmail: "account_id" }
});
// Returns: [{ url: URL, name: "...", toolkit: "gmail" }]
interface MCPToolkitConfig {
toolkit: string; // Toolkit slug (e.g., "gmail", "github")
authConfigId: string; // Auth configuration ID
allowedTools: string[]; // Array of allowed tool IDs
}
interface MCPAuthOptions {
isChatAuth?: boolean; // Use Composio-managed authentication
}
interface MCPGetServerParams {
userId?: string; // User ID (XOR with connectedAccountIds)
connectedAccountIds?: Record<string, string>; // Toolkit to account ID mapping
}
interface McpServerCreateResponse<T> {
id: string; // Server UUID
name: string; // Server name
createdAt?: string; // Creation timestamp
updatedAt?: string; // Last update timestamp
status?: string; // Server status
toolkits: string[]; // List of enabled toolkits
getServer: (params: MCPGetServerParams) => Promise<T>; // Convenience method
}
interface McpListResponse {
items?: Array<{
id: string;
name: string;
createdAt?: string;
updatedAt?: string;
status?: string;
}>;
}
interface McpServerUrlInfo {
url: URL; // MCP server URL
name: string; // Server instance name
toolkit?: string; // Associated toolkit
}
Here's a complete example of using MCP with an AI agent:
import { Composio } from '@composio/core';
import { MastraProvider } from '@composio/mastra';
import { MCPClient } from '@mastra/mcp';
import { Agent } from '@mastra/core/agent';
import { openai } from '@ai-sdk/openai';
// Initialize Composio with Mastra provider
const composio = new Composio({
apiKey: process.env.COMPOSIO_API_KEY,
provider: new MastraProvider(),
});
// Create an MCP server with Gmail toolkit
const mcpServer = await composio.mcp.create(
"gmail-assistant",
[
{
toolkit: "gmail",
authConfigId: "ac_your_auth_config",
allowedTools: ["GMAIL_FETCH_EMAILS", "GMAIL_SEND_EMAIL"]
}
],
{ isChatAuth: true }
);
// Get server URLs for connected accounts
const serverUrls = await mcpServer.getServer({
connectedAccountIds: {
"gmail": "your_connected_account_id"
}
});
// Initialize MCP client with the server URLs
const mcpClient = new MCPClient({
servers: Object.fromEntries(
Object.entries(serverUrls).map(([key, value]) => [
key,
{ url: new URL(value.url) }
])
)
});
// Get available tools from MCP
const tools = await mcpClient.getTools();
// Create an AI agent with MCP tools
const gmailAgent = new Agent({
name: 'Gmail Assistant',
instructions: 'You are a helpful Gmail assistant.',
model: openai('gpt-4'),
tools,
});
// Use the agent
const response = await gmailAgent.generate('Fetch my latest emails');
console.log(response.text);
All MCP methods throw ValidationError when:
try {
const server = await composio.mcp.create("my-server", toolkitConfigs);
} catch (error) {
if (error instanceof ValidationError) {
console.error('Validation failed:', error.message);
console.error('Cause:', error.cause);
}
}