opc-agent 3.0.1 → 4.0.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +404 -74
- package/README.zh-CN.md +82 -0
- package/dist/channels/dingtalk.d.ts +17 -0
- package/dist/channels/dingtalk.js +38 -0
- package/dist/channels/googlechat.d.ts +14 -0
- package/dist/channels/googlechat.js +37 -0
- package/dist/channels/imessage.d.ts +13 -0
- package/dist/channels/imessage.js +28 -0
- package/dist/channels/irc.d.ts +20 -0
- package/dist/channels/irc.js +71 -0
- package/dist/channels/line.d.ts +14 -0
- package/dist/channels/line.js +28 -0
- package/dist/channels/matrix.d.ts +15 -0
- package/dist/channels/matrix.js +28 -0
- package/dist/channels/mattermost.d.ts +18 -0
- package/dist/channels/mattermost.js +49 -0
- package/dist/channels/msteams.d.ts +14 -0
- package/dist/channels/msteams.js +28 -0
- package/dist/channels/nostr.d.ts +14 -0
- package/dist/channels/nostr.js +28 -0
- package/dist/channels/qq.d.ts +15 -0
- package/dist/channels/qq.js +28 -0
- package/dist/channels/signal.d.ts +14 -0
- package/dist/channels/signal.js +28 -0
- package/dist/channels/sms.d.ts +15 -0
- package/dist/channels/sms.js +28 -0
- package/dist/channels/twitch.d.ts +17 -0
- package/dist/channels/twitch.js +59 -0
- package/dist/channels/voice-call.d.ts +27 -0
- package/dist/channels/voice-call.js +82 -0
- package/dist/channels/whatsapp.d.ts +14 -0
- package/dist/channels/whatsapp.js +28 -0
- package/dist/cli/chat.d.ts +2 -0
- package/dist/cli/chat.js +134 -0
- package/dist/cli/setup.d.ts +4 -0
- package/dist/cli/setup.js +303 -0
- package/dist/cli.js +142 -6
- package/dist/core/api-server.d.ts +25 -0
- package/dist/core/api-server.js +286 -0
- package/dist/core/audio.d.ts +50 -0
- package/dist/core/audio.js +68 -0
- package/dist/core/context-discovery.d.ts +16 -0
- package/dist/core/context-discovery.js +107 -0
- package/dist/core/context-refs.d.ts +29 -0
- package/dist/core/context-refs.js +162 -0
- package/dist/core/gateway.d.ts +53 -0
- package/dist/core/gateway.js +80 -0
- package/dist/core/heartbeat.d.ts +19 -0
- package/dist/core/heartbeat.js +50 -0
- package/dist/core/hooks.d.ts +28 -0
- package/dist/core/hooks.js +82 -0
- package/dist/core/ide-bridge.d.ts +53 -0
- package/dist/core/ide-bridge.js +97 -0
- package/dist/core/node-network.d.ts +23 -0
- package/dist/core/node-network.js +77 -0
- package/dist/core/profiles.d.ts +27 -0
- package/dist/core/profiles.js +131 -0
- package/dist/core/sandbox.d.ts +25 -0
- package/dist/core/sandbox.js +84 -1
- package/dist/core/session-manager.d.ts +33 -0
- package/dist/core/session-manager.js +157 -0
- package/dist/core/vision.d.ts +45 -0
- package/dist/core/vision.js +177 -0
- package/dist/hub/brain-seed.d.ts +14 -0
- package/dist/hub/brain-seed.js +77 -0
- package/dist/hub/client.d.ts +25 -0
- package/dist/hub/client.js +44 -0
- package/dist/index.d.ts +66 -1
- package/dist/index.js +95 -3
- package/dist/memory/context-compressor.d.ts +43 -0
- package/dist/memory/context-compressor.js +167 -0
- package/dist/memory/index.d.ts +4 -0
- package/dist/memory/index.js +5 -1
- package/dist/memory/user-profiler.d.ts +50 -0
- package/dist/memory/user-profiler.js +201 -0
- package/dist/providers/index.d.ts +1 -1
- package/dist/providers/index.js +54 -1
- package/dist/scheduler/cron-engine.d.ts +41 -0
- package/dist/scheduler/cron-engine.js +200 -0
- package/dist/scheduler/index.d.ts +3 -0
- package/dist/scheduler/index.js +7 -0
- package/dist/schema/oad.d.ts +12 -12
- package/dist/security/approvals.d.ts +53 -0
- package/dist/security/approvals.js +115 -0
- package/dist/security/elevated.d.ts +41 -0
- package/dist/security/elevated.js +89 -0
- package/dist/security/index.d.ts +6 -0
- package/dist/security/index.js +7 -1
- package/dist/security/secrets.d.ts +34 -0
- package/dist/security/secrets.js +115 -0
- package/dist/skills/builtin/index.d.ts +6 -0
- package/dist/skills/builtin/index.js +402 -0
- package/dist/skills/marketplace.d.ts +30 -0
- package/dist/skills/marketplace.js +142 -0
- package/dist/skills/types.d.ts +34 -0
- package/dist/skills/types.js +16 -0
- package/dist/studio/server.d.ts +25 -0
- package/dist/studio/server.js +780 -0
- package/dist/studio/templates-data.d.ts +21 -0
- package/dist/studio/templates-data.js +148 -0
- package/dist/studio-ui/index.html +2502 -1073
- package/dist/tools/builtin/browser.d.ts +47 -0
- package/dist/tools/builtin/browser.js +284 -0
- package/dist/tools/builtin/home-assistant.d.ts +12 -0
- package/dist/tools/builtin/home-assistant.js +126 -0
- package/dist/tools/builtin/index.d.ts +7 -1
- package/dist/tools/builtin/index.js +23 -2
- package/dist/tools/builtin/rl-tools.d.ts +13 -0
- package/dist/tools/builtin/rl-tools.js +228 -0
- package/dist/tools/builtin/vision.d.ts +6 -0
- package/dist/tools/builtin/vision.js +61 -0
- package/dist/tools/builtin/web-search.d.ts +9 -0
- package/dist/tools/builtin/web-search.js +150 -0
- package/dist/tools/document-processor.d.ts +39 -0
- package/dist/tools/document-processor.js +188 -0
- package/dist/tools/image-generator.d.ts +42 -0
- package/dist/tools/image-generator.js +136 -0
- package/dist/tools/web-scraper.d.ts +20 -0
- package/dist/tools/web-scraper.js +148 -0
- package/dist/tools/web-search.d.ts +51 -0
- package/dist/tools/web-search.js +152 -0
- package/install.ps1 +154 -0
- package/install.sh +164 -0
- package/package.json +63 -52
- package/src/channels/dingtalk.ts +46 -0
- package/src/channels/googlechat.ts +42 -0
- package/src/channels/imessage.ts +32 -0
- package/src/channels/irc.ts +82 -0
- package/src/channels/line.ts +33 -0
- package/src/channels/matrix.ts +34 -0
- package/src/channels/mattermost.ts +57 -0
- package/src/channels/msteams.ts +33 -0
- package/src/channels/nostr.ts +33 -0
- package/src/channels/qq.ts +34 -0
- package/src/channels/signal.ts +33 -0
- package/src/channels/sms.ts +34 -0
- package/src/channels/twitch.ts +65 -0
- package/src/channels/voice-call.ts +100 -0
- package/src/channels/whatsapp.ts +33 -0
- package/src/cli/chat.ts +99 -0
- package/src/cli/setup.ts +314 -0
- package/src/cli.ts +148 -6
- package/src/core/api-server.ts +277 -0
- package/src/core/audio.ts +98 -0
- package/src/core/context-discovery.ts +85 -0
- package/src/core/context-refs.ts +140 -0
- package/src/core/gateway.ts +106 -0
- package/src/core/heartbeat.ts +51 -0
- package/src/core/hooks.ts +105 -0
- package/src/core/ide-bridge.ts +133 -0
- package/src/core/node-network.ts +86 -0
- package/src/core/profiles.ts +122 -0
- package/src/core/sandbox.ts +100 -0
- package/src/core/session-manager.ts +137 -0
- package/src/core/vision.ts +180 -0
- package/src/hub/brain-seed.ts +54 -0
- package/src/hub/client.ts +60 -0
- package/src/index.ts +86 -1
- package/src/memory/context-compressor.ts +189 -0
- package/src/memory/index.ts +4 -0
- package/src/memory/user-profiler.ts +215 -0
- package/src/providers/index.ts +64 -1
- package/src/scheduler/cron-engine.ts +191 -0
- package/src/scheduler/index.ts +2 -0
- package/src/security/approvals.ts +143 -0
- package/src/security/elevated.ts +105 -0
- package/src/security/index.ts +6 -0
- package/src/security/secrets.ts +129 -0
- package/src/skills/builtin/index.ts +408 -0
- package/src/skills/marketplace.ts +113 -0
- package/src/skills/types.ts +42 -0
- package/src/studio/server.ts +1591 -791
- package/src/studio/templates-data.ts +178 -0
- package/src/studio-ui/index.html +2502 -1073
- package/src/tools/builtin/browser.ts +299 -0
- package/src/tools/builtin/home-assistant.ts +116 -0
- package/src/tools/builtin/index.ts +37 -28
- package/src/tools/builtin/rl-tools.ts +243 -0
- package/src/tools/builtin/vision.ts +64 -0
- package/src/tools/builtin/web-search.ts +126 -0
- package/src/tools/document-processor.ts +213 -0
- package/src/tools/image-generator.ts +150 -0
- package/src/tools/web-scraper.ts +179 -0
- package/src/tools/web-search.ts +180 -0
- package/tests/api-server.test.ts +148 -0
- package/tests/approvals.test.ts +89 -0
- package/tests/audio.test.ts +40 -0
- package/tests/browser.test.ts +179 -0
- package/tests/builtin-tools.test.ts +83 -83
- package/tests/channels-extra.test.ts +45 -0
- package/tests/context-compressor.test.ts +172 -0
- package/tests/context-refs.test.ts +121 -0
- package/tests/cron-engine.test.ts +101 -0
- package/tests/document-processor.test.ts +69 -0
- package/tests/e2e-nocode.test.ts +442 -0
- package/tests/elevated.test.ts +69 -0
- package/tests/gateway.test.ts +63 -71
- package/tests/home-assistant.test.ts +40 -0
- package/tests/hooks.test.ts +79 -0
- package/tests/ide-bridge.test.ts +38 -0
- package/tests/image-generator.test.ts +84 -0
- package/tests/node-network.test.ts +74 -0
- package/tests/profiles.test.ts +61 -0
- package/tests/rl-tools.test.ts +93 -0
- package/tests/sandbox-manager.test.ts +46 -0
- package/tests/secrets.test.ts +107 -0
- package/tests/settings-api.test.ts +148 -0
- package/tests/setup.test.ts +73 -0
- package/tests/studio.test.ts +402 -229
- package/tests/tools/builtin-extended.test.ts +138 -138
- package/tests/user-profiler.test.ts +169 -0
- package/tests/v090-features.test.ts +254 -0
- package/tests/vision.test.ts +61 -0
- package/tests/voice-call.test.ts +47 -0
- package/tests/voice-interaction.test.ts +38 -0
- package/tests/web-search.test.ts +155 -0
|
@@ -0,0 +1,299 @@
|
|
|
1
|
+
import type { MCPTool, MCPToolResult } from '../mcp';
|
|
2
|
+
|
|
3
|
+
const IDLE_TIMEOUT_MS = 5 * 60 * 1000;
|
|
4
|
+
const MAX_TEXT_LENGTH = 5000;
|
|
5
|
+
|
|
6
|
+
export class BrowserManager {
|
|
7
|
+
private browser: any = null;
|
|
8
|
+
private page: any = null;
|
|
9
|
+
private lastActivity: number = 0;
|
|
10
|
+
private idleTimer: ReturnType<typeof setTimeout> | null = null;
|
|
11
|
+
private playwrightFactory: (() => any) | null;
|
|
12
|
+
|
|
13
|
+
constructor(playwrightFactory?: () => any) {
|
|
14
|
+
this.playwrightFactory = playwrightFactory || null;
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
private resetIdleTimer(): void {
|
|
18
|
+
this.lastActivity = Date.now();
|
|
19
|
+
if (this.idleTimer) clearTimeout(this.idleTimer);
|
|
20
|
+
this.idleTimer = setTimeout(() => this.close(), IDLE_TIMEOUT_MS);
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
async ensureBrowser(): Promise<any> {
|
|
24
|
+
if (!this.browser) {
|
|
25
|
+
let playwright: any;
|
|
26
|
+
if (this.playwrightFactory) {
|
|
27
|
+
playwright = this.playwrightFactory();
|
|
28
|
+
} else {
|
|
29
|
+
try {
|
|
30
|
+
playwright = require('playwright');
|
|
31
|
+
} catch {
|
|
32
|
+
throw new Error('Install playwright: npm i playwright');
|
|
33
|
+
}
|
|
34
|
+
}
|
|
35
|
+
this.browser = await playwright.chromium.launch({ headless: true });
|
|
36
|
+
const context = await this.browser.newContext();
|
|
37
|
+
this.page = await context.newPage();
|
|
38
|
+
}
|
|
39
|
+
this.resetIdleTimer();
|
|
40
|
+
return this.page;
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
async navigate(url: string): Promise<{ title: string; text: string; url: string }> {
|
|
44
|
+
const page = await this.ensureBrowser();
|
|
45
|
+
await page.goto(url, { waitUntil: 'domcontentloaded', timeout: 30000 });
|
|
46
|
+
const title = await page.title();
|
|
47
|
+
const text = (await page.innerText('body')).slice(0, MAX_TEXT_LENGTH);
|
|
48
|
+
return { title, text, url: page.url() };
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
async click(selector: string): Promise<void> {
|
|
52
|
+
const page = await this.ensureBrowser();
|
|
53
|
+
await page.click(selector, { timeout: 10000 });
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
async type(selector: string, text: string): Promise<void> {
|
|
57
|
+
const page = await this.ensureBrowser();
|
|
58
|
+
await page.fill(selector, text, { timeout: 10000 });
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
async screenshot(): Promise<string> {
|
|
62
|
+
const page = await this.ensureBrowser();
|
|
63
|
+
const buffer = await page.screenshot({ type: 'png' });
|
|
64
|
+
return buffer.toString('base64');
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
async extract(): Promise<{ text: string; links: string[]; images: string[] }> {
|
|
68
|
+
const page = await this.ensureBrowser();
|
|
69
|
+
const text = (await page.innerText('body')).slice(0, MAX_TEXT_LENGTH);
|
|
70
|
+
const links: string[] = await page.$$eval('a[href]', (els: any[]) => els.map((e: any) => e.href).slice(0, 100));
|
|
71
|
+
const images: string[] = await page.$$eval('img[src]', (els: any[]) => els.map((e: any) => e.src).slice(0, 100));
|
|
72
|
+
return { text, links, images };
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
async scroll(direction: 'up' | 'down', amount?: number): Promise<void> {
|
|
76
|
+
const page = await this.ensureBrowser();
|
|
77
|
+
const delta = amount || 500;
|
|
78
|
+
const scrollScript = direction === 'down'
|
|
79
|
+
? `window.scrollBy(0, ${delta})`
|
|
80
|
+
: `window.scrollBy(0, -${delta})`;
|
|
81
|
+
await page.evaluate(scrollScript);
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
async back(): Promise<void> {
|
|
85
|
+
const page = await this.ensureBrowser();
|
|
86
|
+
await page.goBack({ timeout: 10000 });
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
async evaluate(script: string): Promise<any> {
|
|
90
|
+
const page = await this.ensureBrowser();
|
|
91
|
+
return await page.evaluate(script);
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
async getImages(): Promise<Array<{ src: string; alt: string }>> {
|
|
95
|
+
const page = await this.ensureBrowser();
|
|
96
|
+
return await page.$$eval('img', (els: any[]) =>
|
|
97
|
+
els.map((e: any) => ({ src: e.src, alt: e.alt || '' })).slice(0, 200)
|
|
98
|
+
);
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
async waitFor(selector: string, timeout?: number): Promise<boolean> {
|
|
102
|
+
const page = await this.ensureBrowser();
|
|
103
|
+
try {
|
|
104
|
+
await page.waitForSelector(selector, { timeout: timeout || 5000 });
|
|
105
|
+
return true;
|
|
106
|
+
} catch {
|
|
107
|
+
return false;
|
|
108
|
+
}
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
async close(): Promise<void> {
|
|
112
|
+
if (this.idleTimer) {
|
|
113
|
+
clearTimeout(this.idleTimer);
|
|
114
|
+
this.idleTimer = null;
|
|
115
|
+
}
|
|
116
|
+
if (this.browser) {
|
|
117
|
+
try { await this.browser.close(); } catch {}
|
|
118
|
+
this.browser = null;
|
|
119
|
+
this.page = null;
|
|
120
|
+
}
|
|
121
|
+
}
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
// Singleton
|
|
125
|
+
const browserManager = new BrowserManager();
|
|
126
|
+
|
|
127
|
+
function wrapTool(fn: () => Promise<MCPToolResult>): Promise<MCPToolResult> {
|
|
128
|
+
return fn().catch((err: any) => ({
|
|
129
|
+
content: `Browser error: ${err instanceof Error ? err.message : String(err)}`,
|
|
130
|
+
isError: true,
|
|
131
|
+
}));
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
export const browserNavigateTool: MCPTool = {
|
|
135
|
+
name: 'browser_navigate',
|
|
136
|
+
description: 'Navigate to a URL and return page title + text content (truncated)',
|
|
137
|
+
inputSchema: {
|
|
138
|
+
type: 'object',
|
|
139
|
+
properties: {
|
|
140
|
+
url: { type: 'string', description: 'URL to navigate to' },
|
|
141
|
+
},
|
|
142
|
+
required: ['url'],
|
|
143
|
+
},
|
|
144
|
+
execute: (input) => wrapTool(async () => {
|
|
145
|
+
const url = input.url as string;
|
|
146
|
+
if (!url) return { content: 'Missing required parameter: url', isError: true };
|
|
147
|
+
const result = await browserManager.navigate(url);
|
|
148
|
+
return { content: `Title: ${result.title}\nURL: ${result.url}\n\n${result.text}`, isError: false };
|
|
149
|
+
}),
|
|
150
|
+
};
|
|
151
|
+
|
|
152
|
+
export const browserClickTool: MCPTool = {
|
|
153
|
+
name: 'browser_click',
|
|
154
|
+
description: 'Click an element by CSS selector',
|
|
155
|
+
inputSchema: {
|
|
156
|
+
type: 'object',
|
|
157
|
+
properties: {
|
|
158
|
+
selector: { type: 'string', description: 'CSS selector of element to click' },
|
|
159
|
+
},
|
|
160
|
+
required: ['selector'],
|
|
161
|
+
},
|
|
162
|
+
execute: (input) => wrapTool(async () => {
|
|
163
|
+
const selector = input.selector as string;
|
|
164
|
+
if (!selector) return { content: 'Missing required parameter: selector', isError: true };
|
|
165
|
+
await browserManager.click(selector);
|
|
166
|
+
return { content: `Clicked: ${selector}`, isError: false };
|
|
167
|
+
}),
|
|
168
|
+
};
|
|
169
|
+
|
|
170
|
+
export const browserTypeTool: MCPTool = {
|
|
171
|
+
name: 'browser_type',
|
|
172
|
+
description: 'Type text into an element by CSS selector',
|
|
173
|
+
inputSchema: {
|
|
174
|
+
type: 'object',
|
|
175
|
+
properties: {
|
|
176
|
+
selector: { type: 'string', description: 'CSS selector of input element' },
|
|
177
|
+
text: { type: 'string', description: 'Text to type' },
|
|
178
|
+
},
|
|
179
|
+
required: ['selector', 'text'],
|
|
180
|
+
},
|
|
181
|
+
execute: (input) => wrapTool(async () => {
|
|
182
|
+
const selector = input.selector as string;
|
|
183
|
+
const text = input.text as string;
|
|
184
|
+
if (!selector || text === undefined) return { content: 'Missing required parameters: selector, text', isError: true };
|
|
185
|
+
await browserManager.type(selector, text);
|
|
186
|
+
return { content: `Typed into ${selector}`, isError: false };
|
|
187
|
+
}),
|
|
188
|
+
};
|
|
189
|
+
|
|
190
|
+
export const browserScreenshotTool: MCPTool = {
|
|
191
|
+
name: 'browser_screenshot',
|
|
192
|
+
description: 'Take a screenshot of the current page, returned as base64 PNG',
|
|
193
|
+
inputSchema: { type: 'object', properties: {} },
|
|
194
|
+
execute: () => wrapTool(async () => {
|
|
195
|
+
const base64 = await browserManager.screenshot();
|
|
196
|
+
return { content: base64, isError: false, metadata: { encoding: 'base64', mimeType: 'image/png' } };
|
|
197
|
+
}),
|
|
198
|
+
};
|
|
199
|
+
|
|
200
|
+
export const browserExtractTool: MCPTool = {
|
|
201
|
+
name: 'browser_extract',
|
|
202
|
+
description: 'Extract text, links, and images from the current page',
|
|
203
|
+
inputSchema: { type: 'object', properties: {} },
|
|
204
|
+
execute: () => wrapTool(async () => {
|
|
205
|
+
const data = await browserManager.extract();
|
|
206
|
+
return { content: JSON.stringify(data, null, 2), isError: false };
|
|
207
|
+
}),
|
|
208
|
+
};
|
|
209
|
+
|
|
210
|
+
export const browserScrollTool: MCPTool = {
|
|
211
|
+
name: 'browser_scroll',
|
|
212
|
+
description: 'Scroll the page up or down',
|
|
213
|
+
inputSchema: {
|
|
214
|
+
type: 'object',
|
|
215
|
+
properties: {
|
|
216
|
+
direction: { type: 'string', enum: ['up', 'down'], description: 'Scroll direction' },
|
|
217
|
+
amount: { type: 'number', description: 'Pixels to scroll (default 500)' },
|
|
218
|
+
},
|
|
219
|
+
required: ['direction'],
|
|
220
|
+
},
|
|
221
|
+
execute: (input) => wrapTool(async () => {
|
|
222
|
+
const direction = input.direction as 'up' | 'down';
|
|
223
|
+
if (!direction) return { content: 'Missing required parameter: direction', isError: true };
|
|
224
|
+
await browserManager.scroll(direction, input.amount as number | undefined);
|
|
225
|
+
return { content: `Scrolled ${direction}`, isError: false };
|
|
226
|
+
}),
|
|
227
|
+
};
|
|
228
|
+
|
|
229
|
+
export const browserBackTool: MCPTool = {
|
|
230
|
+
name: 'browser_back',
|
|
231
|
+
description: 'Navigate back in browser history',
|
|
232
|
+
inputSchema: { type: 'object', properties: {} },
|
|
233
|
+
execute: () => wrapTool(async () => {
|
|
234
|
+
await browserManager.back();
|
|
235
|
+
return { content: 'Navigated back', isError: false };
|
|
236
|
+
}),
|
|
237
|
+
};
|
|
238
|
+
|
|
239
|
+
export const browserEvalTool: MCPTool = {
|
|
240
|
+
name: 'browser_eval',
|
|
241
|
+
description: 'Execute JavaScript in the page context',
|
|
242
|
+
inputSchema: {
|
|
243
|
+
type: 'object',
|
|
244
|
+
properties: {
|
|
245
|
+
script: { type: 'string', description: 'JavaScript to execute' },
|
|
246
|
+
},
|
|
247
|
+
required: ['script'],
|
|
248
|
+
},
|
|
249
|
+
execute: (input) => wrapTool(async () => {
|
|
250
|
+
const script = input.script as string;
|
|
251
|
+
if (!script) return { content: 'Missing required parameter: script', isError: true };
|
|
252
|
+
const result = await browserManager.evaluate(script);
|
|
253
|
+
return { content: typeof result === 'string' ? result : JSON.stringify(result, null, 2), isError: false };
|
|
254
|
+
}),
|
|
255
|
+
};
|
|
256
|
+
|
|
257
|
+
export const browserGetImagesTool: MCPTool = {
|
|
258
|
+
name: 'browser_get_images',
|
|
259
|
+
description: 'List all images on the current page with src and alt attributes',
|
|
260
|
+
inputSchema: { type: 'object', properties: {} },
|
|
261
|
+
execute: () => wrapTool(async () => {
|
|
262
|
+
const images = await browserManager.getImages();
|
|
263
|
+
return { content: JSON.stringify(images, null, 2), isError: false };
|
|
264
|
+
}),
|
|
265
|
+
};
|
|
266
|
+
|
|
267
|
+
export const browserWaitTool: MCPTool = {
|
|
268
|
+
name: 'browser_wait',
|
|
269
|
+
description: 'Wait for a CSS selector to appear on the page',
|
|
270
|
+
inputSchema: {
|
|
271
|
+
type: 'object',
|
|
272
|
+
properties: {
|
|
273
|
+
selector: { type: 'string', description: 'CSS selector to wait for' },
|
|
274
|
+
timeout: { type: 'number', description: 'Max wait time in ms (default 5000)' },
|
|
275
|
+
},
|
|
276
|
+
required: ['selector'],
|
|
277
|
+
},
|
|
278
|
+
execute: (input) => wrapTool(async () => {
|
|
279
|
+
const selector = input.selector as string;
|
|
280
|
+
if (!selector) return { content: 'Missing required parameter: selector', isError: true };
|
|
281
|
+
const found = await browserManager.waitFor(selector, input.timeout as number | undefined);
|
|
282
|
+
return { content: found ? `Found: ${selector}` : `Timeout waiting for: ${selector}`, isError: !found };
|
|
283
|
+
}),
|
|
284
|
+
};
|
|
285
|
+
|
|
286
|
+
export const browserTools: MCPTool[] = [
|
|
287
|
+
browserNavigateTool,
|
|
288
|
+
browserClickTool,
|
|
289
|
+
browserTypeTool,
|
|
290
|
+
browserScreenshotTool,
|
|
291
|
+
browserExtractTool,
|
|
292
|
+
browserScrollTool,
|
|
293
|
+
browserBackTool,
|
|
294
|
+
browserEvalTool,
|
|
295
|
+
browserGetImagesTool,
|
|
296
|
+
browserWaitTool,
|
|
297
|
+
];
|
|
298
|
+
|
|
299
|
+
export { browserManager };
|
|
@@ -0,0 +1,116 @@
|
|
|
1
|
+
import type { MCPTool, MCPToolResult } from '../mcp';
|
|
2
|
+
|
|
3
|
+
export interface HomeAssistantConfig {
|
|
4
|
+
url: string;
|
|
5
|
+
token: string;
|
|
6
|
+
}
|
|
7
|
+
|
|
8
|
+
let haConfig: HomeAssistantConfig | null = null;
|
|
9
|
+
|
|
10
|
+
export function configureHomeAssistant(config: HomeAssistantConfig): void {
|
|
11
|
+
haConfig = config;
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
async function haFetch(path: string, options?: RequestInit): Promise<any> {
|
|
15
|
+
if (!haConfig) throw new Error('Home Assistant not configured. Call configureHomeAssistant({ url, token }) first.');
|
|
16
|
+
const resp = await fetch(`${haConfig.url}/api${path}`, {
|
|
17
|
+
...options,
|
|
18
|
+
headers: { Authorization: `Bearer ${haConfig.token}`, 'Content-Type': 'application/json', ...options?.headers as Record<string, string> },
|
|
19
|
+
});
|
|
20
|
+
if (!resp.ok) throw new Error(`HA API error: ${resp.status} ${resp.statusText}`);
|
|
21
|
+
return resp.json();
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
export const haGetStates: MCPTool = {
|
|
25
|
+
name: 'ha_get_states',
|
|
26
|
+
description: 'Get all Home Assistant entity states or filter by domain',
|
|
27
|
+
inputSchema: {
|
|
28
|
+
type: 'object',
|
|
29
|
+
properties: { domain: { type: 'string', description: 'Filter by domain (e.g. light, switch)' } },
|
|
30
|
+
},
|
|
31
|
+
async execute(input): Promise<MCPToolResult> {
|
|
32
|
+
try {
|
|
33
|
+
let states = await haFetch('/states');
|
|
34
|
+
if (input.domain) states = states.filter((s: any) => s.entity_id.startsWith(`${input.domain}.`));
|
|
35
|
+
return { content: JSON.stringify(states.map((s: any) => ({ entity_id: s.entity_id, state: s.state, attributes: s.attributes }))) };
|
|
36
|
+
} catch (e: any) { return { content: e.message, isError: true }; }
|
|
37
|
+
},
|
|
38
|
+
};
|
|
39
|
+
|
|
40
|
+
export const haCallService: MCPTool = {
|
|
41
|
+
name: 'ha_call_service',
|
|
42
|
+
description: 'Call a Home Assistant service (turn_on, turn_off, toggle, etc.)',
|
|
43
|
+
inputSchema: {
|
|
44
|
+
type: 'object',
|
|
45
|
+
properties: {
|
|
46
|
+
domain: { type: 'string' },
|
|
47
|
+
service: { type: 'string' },
|
|
48
|
+
entity_id: { type: 'string' },
|
|
49
|
+
data: { type: 'object' },
|
|
50
|
+
},
|
|
51
|
+
required: ['domain', 'service'],
|
|
52
|
+
},
|
|
53
|
+
async execute(input): Promise<MCPToolResult> {
|
|
54
|
+
try {
|
|
55
|
+
const body: any = {};
|
|
56
|
+
if (input.entity_id) body.entity_id = input.entity_id;
|
|
57
|
+
if (input.data) Object.assign(body, input.data);
|
|
58
|
+
const result = await haFetch(`/services/${input.domain}/${input.service}`, {
|
|
59
|
+
method: 'POST', body: JSON.stringify(body),
|
|
60
|
+
});
|
|
61
|
+
return { content: JSON.stringify({ success: true, result }) };
|
|
62
|
+
} catch (e: any) { return { content: e.message, isError: true }; }
|
|
63
|
+
},
|
|
64
|
+
};
|
|
65
|
+
|
|
66
|
+
export const haGetHistory: MCPTool = {
|
|
67
|
+
name: 'ha_get_history',
|
|
68
|
+
description: 'Get entity history for a time period',
|
|
69
|
+
inputSchema: {
|
|
70
|
+
type: 'object',
|
|
71
|
+
properties: {
|
|
72
|
+
entity_id: { type: 'string' },
|
|
73
|
+
start: { type: 'string', description: 'ISO datetime' },
|
|
74
|
+
end: { type: 'string', description: 'ISO datetime' },
|
|
75
|
+
},
|
|
76
|
+
required: ['entity_id'],
|
|
77
|
+
},
|
|
78
|
+
async execute(input): Promise<MCPToolResult> {
|
|
79
|
+
try {
|
|
80
|
+
const start = input.start || new Date(Date.now() - 86400000).toISOString();
|
|
81
|
+
let path = `/history/period/${start}?filter_entity_id=${input.entity_id}`;
|
|
82
|
+
if (input.end) path += `&end_time=${input.end}`;
|
|
83
|
+
const result = await haFetch(path);
|
|
84
|
+
return { content: JSON.stringify(result) };
|
|
85
|
+
} catch (e: any) { return { content: e.message, isError: true }; }
|
|
86
|
+
},
|
|
87
|
+
};
|
|
88
|
+
|
|
89
|
+
export const haAutomation: MCPTool = {
|
|
90
|
+
name: 'ha_automation',
|
|
91
|
+
description: 'Trigger or manage Home Assistant automations',
|
|
92
|
+
inputSchema: {
|
|
93
|
+
type: 'object',
|
|
94
|
+
properties: {
|
|
95
|
+
action: { type: 'string', enum: ['trigger', 'turn_on', 'turn_off', 'list'] },
|
|
96
|
+
automation_id: { type: 'string' },
|
|
97
|
+
},
|
|
98
|
+
required: ['action'],
|
|
99
|
+
},
|
|
100
|
+
async execute(input): Promise<MCPToolResult> {
|
|
101
|
+
try {
|
|
102
|
+
if (input.action === 'list') {
|
|
103
|
+
const states = await haFetch('/states');
|
|
104
|
+
const automations = states.filter((s: any) => s.entity_id.startsWith('automation.'));
|
|
105
|
+
return { content: JSON.stringify(automations.map((a: any) => ({ entity_id: a.entity_id, state: a.state }))) };
|
|
106
|
+
}
|
|
107
|
+
if (!input.automation_id) return { content: 'automation_id required for this action', isError: true };
|
|
108
|
+
const result = await haFetch(`/services/automation/${input.action}`, {
|
|
109
|
+
method: 'POST', body: JSON.stringify({ entity_id: input.automation_id }),
|
|
110
|
+
});
|
|
111
|
+
return { content: JSON.stringify({ success: true, result }) };
|
|
112
|
+
} catch (e: any) { return { content: e.message, isError: true }; }
|
|
113
|
+
},
|
|
114
|
+
};
|
|
115
|
+
|
|
116
|
+
export const homeAssistantTools: MCPTool[] = [haGetStates, haCallService, haGetHistory, haAutomation];
|
|
@@ -1,28 +1,37 @@
|
|
|
1
|
-
import type { MCPTool } from '../mcp';
|
|
2
|
-
import { fileTool } from './file';
|
|
3
|
-
import { webTool } from './web';
|
|
4
|
-
import { shellTool } from './shell';
|
|
5
|
-
import { datetimeTool } from './datetime';
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
1
|
+
import type { MCPTool } from '../mcp';
|
|
2
|
+
import { fileTool } from './file';
|
|
3
|
+
import { webTool } from './web';
|
|
4
|
+
import { shellTool } from './shell';
|
|
5
|
+
import { datetimeTool } from './datetime';
|
|
6
|
+
import { browserTools, BrowserManager, browserManager } from './browser';
|
|
7
|
+
import { visionTools, visionAnalyzeTool, visionExtractTextTool, visionCompareTool } from './vision';
|
|
8
|
+
import { rlTools } from './rl-tools';
|
|
9
|
+
import { homeAssistantTools } from './home-assistant';
|
|
10
|
+
import { webSearchTools, webSearchTool, webReadTool } from './web-search';
|
|
11
|
+
|
|
12
|
+
export { fileTool, webTool, shellTool, datetimeTool, browserTools, BrowserManager, browserManager };
|
|
13
|
+
export { visionTools, visionAnalyzeTool, visionExtractTextTool, visionCompareTool };
|
|
14
|
+
export { rlTools } from './rl-tools';
|
|
15
|
+
export { homeAssistantTools, configureHomeAssistant } from './home-assistant';
|
|
16
|
+
export { webSearchTools, webSearchTool, webReadTool } from './web-search';
|
|
17
|
+
|
|
18
|
+
const ALL_BUILTIN_TOOLS: MCPTool[] = [fileTool, webTool, shellTool, datetimeTool, ...browserTools, ...visionTools, ...rlTools, ...homeAssistantTools, ...webSearchTools];
|
|
19
|
+
|
|
20
|
+
const BUILTIN_MAP = new Map<string, MCPTool>(
|
|
21
|
+
ALL_BUILTIN_TOOLS.map(t => [t.name, t])
|
|
22
|
+
);
|
|
23
|
+
|
|
24
|
+
/**
|
|
25
|
+
* Get all built-in tools.
|
|
26
|
+
*/
|
|
27
|
+
export function getBuiltinTools(): MCPTool[] {
|
|
28
|
+
return [...ALL_BUILTIN_TOOLS];
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
/**
|
|
32
|
+
* Get specific built-in tools by name. If no names given, returns all.
|
|
33
|
+
*/
|
|
34
|
+
export function getBuiltinToolsByName(names?: string[]): MCPTool[] {
|
|
35
|
+
if (!names || names.length === 0) return getBuiltinTools();
|
|
36
|
+
return names.map(n => BUILTIN_MAP.get(n)).filter((t): t is MCPTool => !!t);
|
|
37
|
+
}
|