-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.mjs
More file actions
65 lines (60 loc) · 2.02 KB
/
index.mjs
File metadata and controls
65 lines (60 loc) · 2.02 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
#!/usr/bin/env node
import {createAgent} from 'langchain';
import {ChatGoogleGenerativeAI} from '@langchain/google-genai';
import {loadMcpTools} from '@langchain/mcp-adapters';
import {Client} from '@modelcontextprotocol/sdk/client/index.js';
import {StreamableHTTPClientTransport} from '@modelcontextprotocol/sdk/client/streamableHttp.js';
import {StdioClientTransport} from '@modelcontextprotocol/sdk/client/stdio.js';
const npx = process.platform === 'win32' ? 'npx.cmd' : 'npx';
const initStdioClient = async () => {
const stdioClient = new Client({
name: 'blog.marcnuri.com'
});
const transport = new StdioClientTransport({
command: npx,
args: ['-y', 'kubernetes-mcp-server@latest']
});
await stdioClient.connect(transport);
return stdioClient;
};
const initHttpClient = async () => {
const httpClient = new Client({
name: 'blog.marcnuri.com'
});
const transport = new StreamableHTTPClientTransport(new URL('http://localhost:8080/mcp'));
await httpClient.connect(transport);
return httpClient;
};
const assistant = async () => {
console.log('Starting kubernetes-mcp-server in STDIO mode');
const stdioClient = await initStdioClient();
console.log('Available tools:');
const toolList = await stdioClient.listTools();
toolList.tools.forEach(tool => console.log(` - ${tool.name}: ${tool.description}`));
if (process.argv.includes('--assistant')) {
const model = new ChatGoogleGenerativeAI({
apiKey: process.env['GOOGLE_API_KEY'],
model: 'gemini-2.5-flash',
});
const tools = await loadMcpTools('kubernetes-mcp-server', stdioClient);
const agent = createAgent({
model,
tools,
});
const listPods = await agent.invoke({
messages:[{
role: 'user',
content: 'List all pods in my cluster and output as markdown table'
}]
});
console.log(listPods.messages.slice(-1)[0].content);
}
await stdioClient.close();
};
assistant()
.then(() => {
console.log('done');
})
.catch(err => {
console.error('Error:', err);
});