-
-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathbasic-usage.js
More file actions
executable file
·83 lines (70 loc) · 2.94 KB
/
basic-usage.js
File metadata and controls
executable file
·83 lines (70 loc) · 2.94 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
#!/usr/bin/env node
/**
* Basic usage example for the OpenAPI Directory MCP Server
* This demonstrates how to interact with the server programmatically
*/
import { CacheManager } from '../dist/cache/manager.js';
import { ApiClient } from '../dist/api/client.js';
import { ToolHandler } from '../dist/tools/handler.js';
import { PromptHandler } from '../dist/prompts/handler.js';
async function main() {
console.log('OpenAPI Directory MCP Server - Basic Usage Example');
console.log('==================================================\n');
// Initialize components
const cacheManager = new CacheManager(86400000); // 24 hour TTL
const apiClient = new ApiClient('https://api.apis.guru/v2', cacheManager);
const toolHandler = new ToolHandler();
const promptHandler = new PromptHandler();
try {
// 1. List available tools
console.log('1. Available Tools:');
const { tools } = await toolHandler.listTools();
console.log(` Total tools: ${tools.length}`);
// Show a few tools
tools.slice(0, 5).forEach(tool => {
console.log(` - ${tool.name}: ${tool.description.split('.')[0]}`);
});
console.log(' ...\n');
// Example of calling a tool
console.log(' Example tool call - get_providers:');
const context = { apiClient, cacheManager };
const result = await toolHandler.callTool('get_providers', {}, context);
console.log(` Found ${result.data.length} providers`);
console.log(` First 5: ${result.data.slice(0, 5).join(', ')}...`);
console.log();
// 2. Get resource prompts
console.log('2. Available Resource Prompts:');
const { prompts } = await promptHandler.listPrompts();
console.log(` Total prompts: ${prompts.length}`);
prompts.slice(0, 3).forEach(prompt => {
console.log(` - ${prompt.name}: ${prompt.description}`);
});
console.log(' ...\n');
// 3. Search for APIs using tool
console.log('3. API Search Example:');
const searchResult = await toolHandler.callTool('search_apis',
{ query: 'google' },
context
);
console.log(` Found ${searchResult.results.length} Google APIs`);
if (searchResult.results.length > 0) {
console.log(` First result: ${searchResult.results[0].title}`);
}
console.log();
// 4. Cache statistics
console.log('4. Cache Statistics:');
const cacheStats = await toolHandler.callTool('cache_stats', {}, context);
console.log(` Total entries: ${cacheStats.keys}`);
console.log(` Memory usage: ${((cacheStats.ksize + cacheStats.vsize) / 1024 / 1024).toFixed(2)} MB`);
console.log(` Hits: ${cacheStats.hits}`);
console.log(` Misses: ${cacheStats.misses}`);
console.log();
console.log('Example completed successfully!');
console.log('You can now use the MCP server with MCP-compatible clients.');
} catch (error) {
console.error('Error running example:', error);
process.exit(1);
}
}
// Run the example
main().catch(console.error);