# MCP Toolkit Advanced Examples

This directory contains advanced examples demonstrating how to use the MCP Toolkit with more complex configurations and scenarios.

## Multi-Server Example

The `multi-server.ts` example demonstrates how to:

1. Configure the toolkit to work with multiple servers
2. Handle custom connection settings for different servers
3. Execute tool calls across different servers
4. Execute parallel tool calls
5. Subscribe to resource updates
6. Handle server restarts and implement custom error recovery

## Running the Example

```bash
# Install dependencies
npm install

# Build the toolkit
npm run build

# Run the example
npx tsx examples/advanced/multi-server.ts
```

## Key Concepts

### Toolkit Configuration

```typescript
// Register services with detailed configuration
registerServices({
  servers: {
    'server1': {
      command: 'node',
      args: ['path/to/server1.js'],
      env: { NODE_ENV: 'production' },
      autoRestart: true,
      maxRestarts: 3,
      restartDelay: 1000
    },
    'server2': {
      command: 'node',
      args: ['path/to/server2.js'],
      env: { NODE_ENV: 'production' },
      autoRestart: true
    }
  },
  connections: {
    defaults: {
      transportType: 'stdio',
      requestTimeout: 5000,
      autoReconnect: true
    },
    overrides: {
      'server2': {
        requestTimeout: 10000 // Custom timeout for server2
      }
    }
  },
  toolExecutor: {
    maxRetries: 2,
    retryDelay: 500,
    useExponentialBackoff: true,
    executionTimeout: 10000,
    cacheResults: true,
    cacheTtl: 60000
  },
  resourceManager: {
    maxCacheSize: 5 * 1024 * 1024, // 5MB
    cacheTtl: 300000 // 5 minutes
  }
});
```

### Event Handling

```typescript
// Listen for server status changes
toolkit.on('serverStatus', (event) => {
  console.log(`Server ${event.serverName} status changed to ${event.status}`);
});

// Listen for tool availability changes
toolkit.on('toolsChanged', (event) => {
  console.log(`Available tools changed: ${event.tools.length} tools available`);
});

// Listen for resource updates
toolkit.on('resourceUpdated', (event) => {
  console.log(`Resource updated: ${event.uri}`);
});

// Listen for errors
toolkit.on('error', (event) => {
  console.error(`Error occurred: ${event.error.message}`);
});
```

### Parallel Tool Execution

```typescript
// Execute multiple tool calls in parallel
const parallelCalls = [
  {
    function_name: 'tool1',
    parameters: { param1: 'value1' }
  },
  {
    function_name: 'tool2',
    parameters: { param2: 'value2' }
  }
];

const results = await toolkit.executeToolCalls(parallelCalls);
```

### Server Management

```typescript
// Start all servers
await toolkit.start();

// Get server statuses
const statuses = toolkit.getServerStatuses();

// Restart a specific server
await toolkit.restartServer('server-name');

// Stop all servers
await toolkit.stop();
```

### Resource Subscription

```typescript
// Subscribe to resource updates
await toolkit.subscribeToResource('mcp://server-name/resource-path');

// Listen for updates
toolkit.on('resourceUpdated', (event) => {
  console.log(`Resource ${event.uri} was updated`);
  console.log('New content:', event.content);
});
```