|
| 1 | +import { |
| 2 | + Agent, |
| 3 | + run, |
| 4 | + MCPServerStdio, |
| 5 | + getAllMcpTools, |
| 6 | + withTrace, |
| 7 | +} from '@openai/agents'; |
| 8 | +import * as path from 'node:path'; |
| 9 | +import { createOpenAI } from '@ai-sdk/openai'; |
| 10 | +import { aisdk } from '@openai/agents-extensions'; |
| 11 | + |
| 12 | +const openai = createOpenAI({ |
| 13 | + baseURL: 'https://api.moonshot.cn/v1', |
| 14 | + apiKey: process.env.MOONSHOT_API_KEY, |
| 15 | +}); |
| 16 | + |
| 17 | +const model = aisdk(openai('kimi-k2-turbo-preview')); |
| 18 | + |
| 19 | +async function main() { |
| 20 | + const samplesDir = path.join(__dirname, 'sample_files'); |
| 21 | + |
| 22 | + // Create multiple MCP servers to demonstrate getAllMcpTools |
| 23 | + const filesystemServer = new MCPServerStdio({ |
| 24 | + name: 'Filesystem Server', |
| 25 | + fullCommand: `npx -y @modelcontextprotocol/server-filesystem ${samplesDir}`, |
| 26 | + }); |
| 27 | + |
| 28 | + // Note: This example shows how to use multiple servers |
| 29 | + // In practice, you would have different servers with different tools |
| 30 | + const servers = [filesystemServer]; |
| 31 | + |
| 32 | + // Connect all servers |
| 33 | + for (const server of servers) { |
| 34 | + await server.connect(); |
| 35 | + } |
| 36 | + |
| 37 | + try { |
| 38 | + await withTrace('getAllMcpTools Example', async () => { |
| 39 | + console.log('=== Using getAllMcpTools to fetch all tools ===\n'); |
| 40 | + |
| 41 | + // Method 1: Simple array of servers |
| 42 | + const allTools = await getAllMcpTools(servers); |
| 43 | + console.log( |
| 44 | + `Found ${allTools.length} tools from ${servers.length} server(s):`, |
| 45 | + ); |
| 46 | + allTools.forEach((tool) => { |
| 47 | + const description = |
| 48 | + tool.type === 'function' ? tool.description : 'No description'; |
| 49 | + console.log(`- ${tool.name}: ${description}`); |
| 50 | + }); |
| 51 | + |
| 52 | + console.log('\n=== Using getAllMcpTools with options object ===\n'); |
| 53 | + |
| 54 | + // Method 2: Using options object (recommended for more control) |
| 55 | + const allToolsWithOptions = await getAllMcpTools({ |
| 56 | + mcpServers: servers, |
| 57 | + convertSchemasToStrict: true, // Convert schemas to strict mode |
| 58 | + }); |
| 59 | + |
| 60 | + console.log( |
| 61 | + `Found ${allToolsWithOptions.length} tools with strict schemas:`, |
| 62 | + ); |
| 63 | + allToolsWithOptions.forEach((tool) => { |
| 64 | + const description = |
| 65 | + tool.type === 'function' ? tool.description : 'No description'; |
| 66 | + console.log(`- ${tool.name}: ${description}`); |
| 67 | + }); |
| 68 | + |
| 69 | + console.log('\n=== Creating agent with pre-fetched tools ===\n'); |
| 70 | + |
| 71 | + // Create agent using the pre-fetched tools |
| 72 | + const agent = new Agent({ |
| 73 | + name: 'MCP Assistant with Pre-fetched Tools', |
| 74 | + instructions: |
| 75 | + 'Use the available tools to help the user with file operations.', |
| 76 | + tools: allTools, // Use pre-fetched tools instead of mcpServers |
| 77 | + model, |
| 78 | + }); |
| 79 | + |
| 80 | + // Test the agent |
| 81 | + const message = 'List the available files and read one of them.'; |
| 82 | + console.log(`Running: ${message}\n`); |
| 83 | + const result = await run(agent, message); |
| 84 | + console.log(result.finalOutput); |
| 85 | + |
| 86 | + console.log( |
| 87 | + '\n=== Demonstrating tool filtering with getAllMcpTools ===\n', |
| 88 | + ); |
| 89 | + |
| 90 | + // Add tool filter to one of the servers |
| 91 | + filesystemServer.toolFilter = { |
| 92 | + allowedToolNames: ['read_file'], // Only allow read_file tool |
| 93 | + }; |
| 94 | + |
| 95 | + // Note: For callable filters to work, you need to pass runContext and agent |
| 96 | + // This is typically done internally when the agent runs |
| 97 | + const filteredTools = await getAllMcpTools({ |
| 98 | + mcpServers: servers, |
| 99 | + convertSchemasToStrict: false, |
| 100 | + // runContext and agent would normally be provided by the agent runtime |
| 101 | + // For demo purposes, we're showing the structure |
| 102 | + }); |
| 103 | + |
| 104 | + console.log(`After filtering, found ${filteredTools.length} tools:`); |
| 105 | + filteredTools.forEach((tool) => { |
| 106 | + console.log(`- ${tool.name}`); |
| 107 | + }); |
| 108 | + }); |
| 109 | + } finally { |
| 110 | + // Clean up - close all servers |
| 111 | + for (const server of servers) { |
| 112 | + await server.close(); |
| 113 | + } |
| 114 | + } |
| 115 | +} |
| 116 | + |
| 117 | +main().catch((err) => { |
| 118 | + console.error('Error:', err); |
| 119 | + process.exit(1); |
| 120 | +}); |
0 commit comments