@choonkeat/agent-reverse-proxy 0.1.4 → 0.2.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 +120 -0
- package/package.json +7 -7
package/README.md
CHANGED
|
@@ -83,6 +83,126 @@ Listen for console output, errors, and network requests for a specified duration
|
|
|
83
83
|
| `APP_PORT` or `PORT` | `--app-port` | 3000 |
|
|
84
84
|
| `PROXY_PORT` | `--proxy-port` | 20000 + app port |
|
|
85
85
|
|
|
86
|
+
## Embedding as a Go library
|
|
87
|
+
|
|
88
|
+
The root package is `agentproxy` — import it to mount the proxy inside your own server.
|
|
89
|
+
|
|
90
|
+
### Fixed target (proxy all requests to one backend)
|
|
91
|
+
|
|
92
|
+
```go
|
|
93
|
+
package main
|
|
94
|
+
|
|
95
|
+
import (
|
|
96
|
+
"log"
|
|
97
|
+
"net/http"
|
|
98
|
+
"net/url"
|
|
99
|
+
|
|
100
|
+
agentproxy "agent-reverse-proxy"
|
|
101
|
+
"github.com/modelcontextprotocol/go-sdk/mcp"
|
|
102
|
+
)
|
|
103
|
+
|
|
104
|
+
func main() {
|
|
105
|
+
target, _ := url.Parse("http://localhost:3000")
|
|
106
|
+
|
|
107
|
+
// 1. Create the proxy
|
|
108
|
+
proxy, err := agentproxy.New(agentproxy.Config{
|
|
109
|
+
Target: target,
|
|
110
|
+
ToolPrefix: "preview",
|
|
111
|
+
ThemeCookie: "my-theme",
|
|
112
|
+
})
|
|
113
|
+
if err != nil {
|
|
114
|
+
log.Fatal(err)
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
// 2. Create an MCP server and register tools + resources
|
|
118
|
+
mcpServer := mcp.NewServer(&mcp.Implementation{
|
|
119
|
+
Name: "my-app",
|
|
120
|
+
Version: "1.0.0",
|
|
121
|
+
}, nil)
|
|
122
|
+
proxy.RegisterTools(mcpServer)
|
|
123
|
+
proxy.RegisterResources(mcpServer)
|
|
124
|
+
|
|
125
|
+
// 3. Wire up all handlers
|
|
126
|
+
mux := http.NewServeMux()
|
|
127
|
+
mux.Handle("/mcp", proxy.MCPHandler(mcpServer)) // MCP-over-HTTP
|
|
128
|
+
mux.Handle("/", proxy) // reverse proxy + debug endpoints
|
|
129
|
+
|
|
130
|
+
log.Fatal(http.ListenAndServe(":8080", mux))
|
|
131
|
+
}
|
|
132
|
+
```
|
|
133
|
+
|
|
134
|
+
`proxy` (`http.Handler`) serves:
|
|
135
|
+
|
|
136
|
+
| Path | What |
|
|
137
|
+
|------|------|
|
|
138
|
+
| `/__agent-reverse-proxy-debug__/inject.js` | Debug instrumentation script |
|
|
139
|
+
| `/__agent-reverse-proxy-debug__/ws` | WebSocket for iframe debug clients |
|
|
140
|
+
| `/__agent-reverse-proxy-debug__/agent` | WebSocket for agent connections |
|
|
141
|
+
| `/__agent-reverse-proxy-debug__/ui` | WebSocket for UI observers |
|
|
142
|
+
| `/__agent-reverse-proxy-debug__/open` | Open a URL in the Preview pane |
|
|
143
|
+
| `/__agent-reverse-proxy-debug__/shell` | Double-iframe shell page |
|
|
144
|
+
| `/*` | Reverse proxy to target (with HTML injection) |
|
|
145
|
+
|
|
146
|
+
`proxy.MCPHandler(mcpServer)` (`http.Handler`) serves the StreamableHTTP MCP endpoint. Mount it wherever you want (e.g. `/mcp`, `/api/mcp`).
|
|
147
|
+
|
|
148
|
+
### Dynamic target (target URL in the request path)
|
|
149
|
+
|
|
150
|
+
Set `Target: nil` — the proxy extracts the backend from the path as `/{scheme}/{host:port}/{path...}`:
|
|
151
|
+
|
|
152
|
+
```go
|
|
153
|
+
proxy, _ := agentproxy.New(agentproxy.Config{
|
|
154
|
+
Target: nil, // dynamic mode
|
|
155
|
+
ToolPrefix: "preview",
|
|
156
|
+
})
|
|
157
|
+
|
|
158
|
+
// GET /http/localhost:3000/hello → proxies to http://localhost:3000/hello
|
|
159
|
+
// GET /https/api.example.com:443/v1 → proxies to https://api.example.com:443/v1
|
|
160
|
+
```
|
|
161
|
+
|
|
162
|
+
### Mounting at a base path
|
|
163
|
+
|
|
164
|
+
Set `BasePath` to mount the proxy under a prefix. All debug endpoints and proxy routes are served relative to this path:
|
|
165
|
+
|
|
166
|
+
```go
|
|
167
|
+
proxy, _ := agentproxy.New(agentproxy.Config{
|
|
168
|
+
BasePath: "/preview",
|
|
169
|
+
Target: target,
|
|
170
|
+
ToolPrefix: "preview",
|
|
171
|
+
})
|
|
172
|
+
|
|
173
|
+
mux := http.NewServeMux()
|
|
174
|
+
mux.Handle("/preview/", proxy) // note: trailing slash to match sub-paths
|
|
175
|
+
// Now:
|
|
176
|
+
// /preview/ → proxied to target /
|
|
177
|
+
// /preview/hello → proxied to target /hello
|
|
178
|
+
// /preview/__agent-reverse-proxy-debug__/inject.js → debug script
|
|
179
|
+
// /preview/mcp → NOT handled (mount MCPHandler separately if needed)
|
|
180
|
+
```
|
|
181
|
+
|
|
182
|
+
The injected `<script src>` tag and the WebSocket URL in `inject.js` automatically include the base path — no extra configuration needed.
|
|
183
|
+
|
|
184
|
+
### API summary
|
|
185
|
+
|
|
186
|
+
```go
|
|
187
|
+
// Create a proxy instance
|
|
188
|
+
proxy, err := agentproxy.New(cfg agentproxy.Config) (*agentproxy.Proxy, error)
|
|
189
|
+
|
|
190
|
+
// http.Handler — serves reverse proxy + all debug endpoints
|
|
191
|
+
proxy.ServeHTTP(w, r)
|
|
192
|
+
|
|
193
|
+
// Register MCP tools (browser_snapshot, browser_console_messages)
|
|
194
|
+
proxy.RegisterTools(mcpServer *mcp.Server)
|
|
195
|
+
|
|
196
|
+
// Register MCP resources (reference, help)
|
|
197
|
+
proxy.RegisterResources(mcpServer *mcp.Server)
|
|
198
|
+
|
|
199
|
+
// Returns an http.Handler for the MCP-over-HTTP endpoint
|
|
200
|
+
proxy.MCPHandler(mcpServer *mcp.Server) http.Handler
|
|
201
|
+
|
|
202
|
+
// Access the DebugHub for direct message routing
|
|
203
|
+
proxy.Hub() *agentproxy.DebugHub
|
|
204
|
+
```
|
|
205
|
+
|
|
86
206
|
## Features
|
|
87
207
|
|
|
88
208
|
- Reverse proxies HTTP and WebSocket connections
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@choonkeat/agent-reverse-proxy",
|
|
3
|
-
"version": "0.1
|
|
3
|
+
"version": "0.2.1",
|
|
4
4
|
"description": "MCP server with reverse proxy and debug instrumentation for app preview",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"bin": {
|
|
@@ -16,11 +16,11 @@
|
|
|
16
16
|
},
|
|
17
17
|
"license": "MIT",
|
|
18
18
|
"optionalDependencies": {
|
|
19
|
-
"@choonkeat/agent-reverse-proxy-linux-x64": "0.1
|
|
20
|
-
"@choonkeat/agent-reverse-proxy-linux-arm64": "0.1
|
|
21
|
-
"@choonkeat/agent-reverse-proxy-darwin-x64": "0.1
|
|
22
|
-
"@choonkeat/agent-reverse-proxy-darwin-arm64": "0.1
|
|
23
|
-
"@choonkeat/agent-reverse-proxy-win32-x64": "0.1
|
|
24
|
-
"@choonkeat/agent-reverse-proxy-win32-arm64": "0.1
|
|
19
|
+
"@choonkeat/agent-reverse-proxy-linux-x64": "0.2.1",
|
|
20
|
+
"@choonkeat/agent-reverse-proxy-linux-arm64": "0.2.1",
|
|
21
|
+
"@choonkeat/agent-reverse-proxy-darwin-x64": "0.2.1",
|
|
22
|
+
"@choonkeat/agent-reverse-proxy-darwin-arm64": "0.2.1",
|
|
23
|
+
"@choonkeat/agent-reverse-proxy-win32-x64": "0.2.1",
|
|
24
|
+
"@choonkeat/agent-reverse-proxy-win32-arm64": "0.2.1"
|
|
25
25
|
}
|
|
26
26
|
}
|