opencode-browser 1.0.0

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.
Files changed (4) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +315 -0
  3. package/index.ts +113 -0
  4. package/package.json +41 -0
package/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2026
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md ADDED
@@ -0,0 +1,315 @@
1
+ # OpenCode Browser MCP Plugin
2
+
3
+ An OpenCode plugin that integrates [Browser MCP](https://browsermcp.io) to enable browser automation capabilities within OpenCode. This plugin allows the AI to control a browser, navigate websites, fill forms, click elements, and perform other browser automation tasks.
4
+
5
+ ## Features
6
+
7
+ - Full browser automation support through Browser MCP
8
+ - Automatic detection of browser-related tasks
9
+ - Context preservation for browser state across session compactions
10
+ - Logging and monitoring of browser automation activities
11
+ - Seamless integration with OpenCode's existing tools
12
+
13
+ ## Prerequisites
14
+
15
+ Before using this plugin, you need:
16
+
17
+ 1. **Node.js** installed on your system
18
+ 2. **OpenCode** installed and configured
19
+ 3. **Browser MCP extension** installed in your browser (Chrome/Edge)
20
+
21
+ ## Installation
22
+
23
+ ### Step 1: Install Browser MCP Extension
24
+
25
+ 1. Visit [https://browsermcp.io/install](https://browsermcp.io/install)
26
+ 2. Install the Browser MCP extension for your browser (Chrome or Edge)
27
+ 3. Follow the extension setup instructions
28
+
29
+ ### Step 2: Configure Browser MCP Server in OpenCode
30
+
31
+ Add the Browser MCP server configuration to your `opencode.json`:
32
+
33
+ ```json
34
+ {
35
+ "$schema": "https://opencode.ai/config.json",
36
+ "mcp": {
37
+ "browsermcp": {
38
+ "type": "local",
39
+ "command": ["npx", "-y", "@browsermcp/mcp@latest"],
40
+ "enabled": true
41
+ }
42
+ }
43
+ }
44
+ ```
45
+
46
+ ### Step 3: Install the Plugin
47
+
48
+ #### Recommended: Install from npm
49
+
50
+ Simply add the plugin to your `opencode.json`:
51
+
52
+ ```json
53
+ {
54
+ "$schema": "https://opencode.ai/config.json",
55
+ "plugin": ["opencode-browser"],
56
+ "mcp": {
57
+ "browsermcp": {
58
+ "type": "local",
59
+ "command": ["npx", "-y", "@browsermcp/mcp@latest"],
60
+ "enabled": true
61
+ }
62
+ }
63
+ }
64
+ ```
65
+
66
+ OpenCode will automatically download and install the plugin from npm when it starts. No manual file copying required!
67
+
68
+ #### Alternative: Install Locally (for development/testing)
69
+
70
+ If you want to modify the plugin or test changes:
71
+
72
+ **For global installation:**
73
+ ```bash
74
+ mkdir -p ~/.config/opencode/plugin
75
+ cp index.ts ~/.config/opencode/plugin/browser-mcp.ts
76
+ ```
77
+
78
+ **For project-specific installation:**
79
+ ```bash
80
+ mkdir -p .opencode/plugin
81
+ cp index.ts .opencode/plugin/browser-mcp.ts
82
+ ```
83
+
84
+ The plugin will be automatically loaded on OpenCode startup.
85
+
86
+ ## Configuration
87
+
88
+ ### Basic Configuration
89
+
90
+ The minimal configuration requires only the MCP server setup:
91
+
92
+ ```json
93
+ {
94
+ "$schema": "https://opencode.ai/config.json",
95
+ "mcp": {
96
+ "browsermcp": {
97
+ "type": "local",
98
+ "command": ["npx", "-y", "@browsermcp/mcp@latest"],
99
+ "enabled": true
100
+ }
101
+ }
102
+ }
103
+ ```
104
+
105
+ ### Advanced Configuration
106
+
107
+ For more control, you can disable Browser MCP tools globally and enable them per agent:
108
+
109
+ ```json
110
+ {
111
+ "$schema": "https://opencode.ai/config.json",
112
+ "mcp": {
113
+ "browsermcp": {
114
+ "type": "local",
115
+ "command": ["npx", "-y", "@browsermcp/mcp@latest"],
116
+ "enabled": true
117
+ }
118
+ },
119
+ "tools": {
120
+ "browsermcp_*": false
121
+ },
122
+ "agent": {
123
+ "browser-agent": {
124
+ "tools": {
125
+ "browsermcp_*": true
126
+ }
127
+ }
128
+ }
129
+ }
130
+ ```
131
+
132
+ ### Environment Variables
133
+
134
+ If you need to pass environment variables to the Browser MCP server:
135
+
136
+ ```json
137
+ {
138
+ "$schema": "https://opencode.ai/config.json",
139
+ "mcp": {
140
+ "browsermcp": {
141
+ "type": "local",
142
+ "command": ["npx", "-y", "@browsermcp/mcp@latest"],
143
+ "enabled": true,
144
+ "environment": {
145
+ "BROWSER_MCP_DEBUG": "true"
146
+ }
147
+ }
148
+ }
149
+ }
150
+ ```
151
+
152
+ ## Usage
153
+
154
+ Once installed and configured, you can use browser automation in your OpenCode prompts:
155
+
156
+ ### Basic Browser Navigation
157
+
158
+ ```
159
+ Navigate to https://github.com and search for "opencode"
160
+ ```
161
+
162
+ ### Form Filling
163
+
164
+ ```
165
+ Go to the contact form at https://example.com/contact and fill in:
166
+ - Name: John Doe
167
+ - Email: john@example.com
168
+ - Message: Hello from OpenCode!
169
+ Then submit the form.
170
+ ```
171
+
172
+ ### Web Scraping
173
+
174
+ ```
175
+ Visit https://news.ycombinator.com and get the titles of the top 5 stories
176
+ ```
177
+
178
+ ### Complex Automation
179
+
180
+ ```
181
+ Go to https://example.com/login, log in with the test credentials,
182
+ navigate to the dashboard, and screenshot the main metrics panel
183
+ ```
184
+
185
+ ### Prompt Tips
186
+
187
+ For best results when using browser automation:
188
+
189
+ 1. **Be specific** about URLs and actions
190
+ 2. **Use step-by-step instructions** for complex workflows
191
+ 3. **Specify selectors** when needed (CSS selectors, text content, etc.)
192
+ 4. **Ask for verification** when critical (screenshots, text content)
193
+
194
+ You can also add browser automation guidelines to your `AGENTS.md` file:
195
+
196
+ ```markdown
197
+ ## Browser Automation
198
+
199
+ When performing browser automation tasks:
200
+ - Always confirm the page has loaded before interacting
201
+ - Use descriptive selectors (prefer text content over CSS selectors)
202
+ - Take screenshots when verification is needed
203
+ - Handle errors gracefully (page not found, element not visible, etc.)
204
+ - Close tabs when the task is complete
205
+ ```
206
+
207
+ ## Plugin Features
208
+
209
+ ### Automatic Browser Tool Detection
210
+
211
+ The plugin automatically detects when Browser MCP tools are being used and logs the activity.
212
+
213
+ ### Session Context Preservation
214
+
215
+ During session compaction, the plugin preserves browser automation context, ensuring the AI remembers:
216
+ - Browser interactions that occurred
217
+ - Current browser state considerations
218
+ - Need to verify page state before assumptions
219
+
220
+ ### Tool Execution Hooks
221
+
222
+ The plugin hooks into tool execution to:
223
+ - Log browser automation activities
224
+ - Monitor tool arguments and results
225
+ - Enable custom post-processing of browser actions
226
+
227
+ ## Troubleshooting
228
+
229
+ ### Browser MCP Not Working
230
+
231
+ 1. **Check extension is installed**: Open your browser and verify the Browser MCP extension is installed and enabled
232
+ 2. **Verify MCP server config**: Ensure your `opencode.json` has the correct MCP configuration
233
+ 3. **Check Node.js**: Ensure Node.js is installed: `node --version`
234
+ 4. **Test MCP connection**: Restart OpenCode after adding the MCP configuration
235
+
236
+ ### Plugin Not Loading
237
+
238
+ 1. **Check file location**: Ensure the plugin file is in the correct directory
239
+ 2. **Check file name**: Plugin files should end in `.ts` or `.js`
240
+ 3. **Check syntax**: Ensure the TypeScript/JavaScript syntax is valid
241
+ 4. **Check logs**: Look for plugin initialization messages in OpenCode output
242
+
243
+ ### Tools Not Available
244
+
245
+ 1. **Check MCP server status**: Ensure the Browser MCP server started successfully
246
+ 2. **Check tool configuration**: Verify tools aren't disabled in your config
247
+ 3. **Restart OpenCode**: Try restarting OpenCode after configuration changes
248
+
249
+ ### Debug Mode
250
+
251
+ Enable debug logging by modifying the plugin or checking OpenCode logs:
252
+
253
+ ```bash
254
+ # Check OpenCode logs
255
+ opencode --verbose
256
+ ```
257
+
258
+ ## Development
259
+
260
+ ### Building from Source
261
+
262
+ If you want to modify the plugin:
263
+
264
+ 1. Clone the repository
265
+ 2. Make your changes to `index.ts`
266
+ 3. Test locally by copying to your OpenCode plugin directory
267
+ 4. Submit a PR if you'd like to contribute!
268
+
269
+ ### Plugin Architecture
270
+
271
+ The plugin uses OpenCode's plugin system hooks:
272
+
273
+ - `session.created`: Initialize session-specific state
274
+ - `tool.execute.before`: Pre-process browser tool calls
275
+ - `tool.execute.after`: Post-process browser tool results
276
+ - `experimental.session.compacting`: Preserve browser context
277
+ - `event`: Handle OpenCode events
278
+
279
+ ## Contributing
280
+
281
+ Contributions are welcome! Please:
282
+
283
+ 1. Fork the repository
284
+ 2. Create a feature branch
285
+ 3. Make your changes
286
+ 4. Submit a pull request
287
+
288
+ ## Resources
289
+
290
+ - [Browser MCP Documentation](https://docs.browsermcp.io/)
291
+ - [OpenCode Documentation](https://opencode.ai/docs/)
292
+ - [OpenCode Plugin Guide](https://opencode.ai/docs/plugins/)
293
+ - [MCP Servers in OpenCode](https://opencode.ai/docs/mcp-servers/)
294
+
295
+ ## License
296
+
297
+ MIT License - See LICENSE file for details
298
+
299
+ ## Support
300
+
301
+ For issues and questions:
302
+
303
+ - Browser MCP issues: [Browser MCP GitHub](https://github.com/browsermcp/browser-mcp)
304
+ - OpenCode issues: [OpenCode GitHub](https://github.com/anomalyco/opencode)
305
+ - Plugin issues: Open an issue in this repository
306
+
307
+ ## Changelog
308
+
309
+ ### 1.0.0
310
+
311
+ - Initial release
312
+ - Browser MCP integration
313
+ - Session context preservation
314
+ - Tool execution logging
315
+ - Event handling
package/index.ts ADDED
@@ -0,0 +1,113 @@
1
+ import type { Plugin } from "@opencode-ai/plugin"
2
+
3
+ /**
4
+ * OpenCode Plugin for Browser MCP Integration
5
+ *
6
+ * This plugin integrates Browser MCP (https://browsermcp.io) to enable browser automation
7
+ * capabilities within OpenCode. It allows the AI to control a browser, navigate websites,
8
+ * fill forms, click elements, and perform other browser automation tasks.
9
+ *
10
+ * Setup:
11
+ * 1. Install the Browser MCP extension in your browser
12
+ * 2. Configure the MCP server in your opencode.json (see README.md)
13
+ * 3. Enable this plugin
14
+ *
15
+ * The plugin automatically detects browser-related requests and provides context hints
16
+ * to help the AI use Browser MCP tools effectively.
17
+ */
18
+ export const BrowserMCPPlugin: Plugin = async (ctx) => {
19
+ const { client, project } = ctx
20
+
21
+ console.log("Browser MCP Plugin initialized!")
22
+
23
+ // Track if we've informed the user about browser automation capabilities
24
+ let browserCapabilitiesShown = false
25
+
26
+ return {
27
+ /**
28
+ * Hook into session creation to inject browser automation context
29
+ */
30
+ "session.created": async ({ session }) => {
31
+ console.log(`Browser MCP Plugin: Session ${session.id} created`)
32
+ },
33
+
34
+ /**
35
+ * Hook before tool execution to provide browser-specific guidance
36
+ */
37
+ "tool.execute.before": async (input, output) => {
38
+ // Detect if a browser-related MCP tool is being called
39
+ if (input.tool.startsWith("browsermcp_")) {
40
+ console.log(`Browser MCP Plugin: Executing browser tool: ${input.tool}`)
41
+
42
+ // Log the tool execution for debugging
43
+ if (output.args) {
44
+ console.log(`Browser MCP Plugin: Tool arguments:`, JSON.stringify(output.args, null, 2))
45
+ }
46
+ }
47
+ },
48
+
49
+ /**
50
+ * Hook after tool execution to handle browser automation results
51
+ */
52
+ "tool.execute.after": async (input, output) => {
53
+ if (input.tool.startsWith("browsermcp_")) {
54
+ console.log(`Browser MCP Plugin: Completed browser tool: ${input.tool}`)
55
+
56
+ // You can add custom post-processing here, such as:
57
+ // - Logging results to a file
58
+ // - Sending notifications
59
+ // - Updating external systems
60
+ }
61
+ },
62
+
63
+ /**
64
+ * Hook to add browser automation context during session compaction
65
+ * This helps preserve browser-related context across long sessions
66
+ */
67
+ "experimental.session.compacting": async (input, output) => {
68
+ // Check if any browser automation was performed in this session
69
+ const hasBrowserTools = input.messages.some(msg =>
70
+ msg.content?.some(part =>
71
+ part.type === "tool_use" && part.name?.startsWith("browsermcp_")
72
+ )
73
+ )
74
+
75
+ if (hasBrowserTools) {
76
+ output.context.push(`## Browser Automation Context
77
+
78
+ The Browser MCP integration has been used in this session. When resuming:
79
+ - Browser state may have changed since last interaction
80
+ - Browser tabs opened during automation may still be active
81
+ - Consider checking current browser state before making assumptions
82
+ - Use Browser MCP tools to verify page state when needed`)
83
+ }
84
+ },
85
+
86
+ /**
87
+ * Hook into TUI toast notifications to show browser-specific tips
88
+ */
89
+ "tui.toast.show": async (input, output) => {
90
+ // You could customize toast messages related to browser automation here
91
+ },
92
+
93
+ /**
94
+ * Event handler for various OpenCode events
95
+ */
96
+ event: async ({ event }) => {
97
+ // Handle session idle - could be used to close browser tabs
98
+ if (event.type === "session.idle") {
99
+ console.log("Browser MCP Plugin: Session is idle")
100
+ }
101
+
102
+ // Handle session errors - could help debug browser automation issues
103
+ if (event.type === "session.error") {
104
+ console.log("Browser MCP Plugin: Session error occurred")
105
+ }
106
+ }
107
+ }
108
+ }
109
+
110
+ /**
111
+ * Default export for the plugin
112
+ */
113
+ export default BrowserMCPPlugin
package/package.json ADDED
@@ -0,0 +1,41 @@
1
+ {
2
+ "name": "opencode-browser",
3
+ "version": "1.0.0",
4
+ "description": "OpenCode plugin that integrates Browser MCP for browser automation",
5
+ "type": "module",
6
+ "main": "index.ts",
7
+ "keywords": [
8
+ "opencode",
9
+ "opencode-plugin",
10
+ "plugin",
11
+ "browser",
12
+ "browser-automation",
13
+ "mcp",
14
+ "model-context-protocol",
15
+ "automation",
16
+ "browser-control",
17
+ "web-automation",
18
+ "puppeteer-alternative"
19
+ ],
20
+ "author": "",
21
+ "license": "MIT",
22
+ "peerDependencies": {
23
+ "@opencode-ai/plugin": "*"
24
+ },
25
+ "repository": {
26
+ "type": "git",
27
+ "url": "git+https://github.com/jach/opencode-browser.git"
28
+ },
29
+ "homepage": "https://github.com/jach/opencode-browser#readme",
30
+ "bugs": {
31
+ "url": "https://github.com/jach/opencode-browser/issues"
32
+ },
33
+ "files": [
34
+ "index.ts",
35
+ "README.md",
36
+ "LICENSE"
37
+ ],
38
+ "engines": {
39
+ "node": ">=18.0.0"
40
+ }
41
+ }