mirror of
https://github.com/codeany-ai/open-agent-sdk-typescript.git
synced 2026-04-25 07:00:49 +09:00
67e120b2ed
Open-source Agent SDK with 30+ built-in tools, MCP integration, multi-turn sessions, subagents, and streaming support. Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
50 lines
1.3 KiB
TypeScript
50 lines
1.3 KiB
TypeScript
/**
|
|
* Example 6: MCP Server Integration
|
|
*
|
|
* Connects to an MCP (Model Context Protocol) server and uses
|
|
* its tools through the agent. This example uses the filesystem
|
|
* MCP server as a demonstration.
|
|
*
|
|
* Prerequisites:
|
|
* npm install -g @modelcontextprotocol/server-filesystem
|
|
*
|
|
* Run: npx tsx examples/06-mcp-server.ts
|
|
*/
|
|
import { createAgent } from '../src/index.js'
|
|
|
|
async function main() {
|
|
console.log('--- Example 6: MCP Server Integration ---\n')
|
|
|
|
const agent = createAgent({
|
|
model: process.env.CODEANY_MODEL || 'claude-sonnet-4-6',
|
|
maxTurns: 10,
|
|
mcpServers: {
|
|
filesystem: {
|
|
command: 'npx',
|
|
args: ['-y', '@modelcontextprotocol/server-filesystem', '/tmp'],
|
|
},
|
|
},
|
|
})
|
|
|
|
console.log('Connecting to MCP filesystem server...\n')
|
|
|
|
const result = await agent.prompt(
|
|
'Use the filesystem MCP tools to list files in /tmp. Be brief.',
|
|
)
|
|
|
|
console.log(`Answer: ${result.text}`)
|
|
console.log(`Turns: ${result.num_turns}`)
|
|
|
|
await agent.close()
|
|
}
|
|
|
|
main().catch(e => {
|
|
console.error('Error:', e.message)
|
|
if (e.message.includes('ENOENT') || e.message.includes('not found')) {
|
|
console.error(
|
|
'\nMCP server not found. Install it with:\n' +
|
|
' npm install -g @modelcontextprotocol/server-filesystem\n',
|
|
)
|
|
}
|
|
})
|