context-drop 0.0.0-stage → 0.2.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.
- package/MIT-LICENSE +21 -0
- package/README.md +709 -2
- package/app/assets/javascripts/context_drop/context_drop.js +8936 -0
- package/dist/app/javascript/context_drop/api-client.d.ts +41 -0
- package/dist/app/javascript/context_drop/api-client.js +279 -0
- package/dist/app/javascript/context_drop/element-picker.d.ts +111 -0
- package/dist/app/javascript/context_drop/element-picker.js +1428 -0
- package/dist/app/javascript/context_drop/icons.d.ts +32 -0
- package/dist/app/javascript/context_drop/icons.js +32 -0
- package/dist/app/javascript/context_drop/index.d.ts +7 -0
- package/dist/app/javascript/context_drop/index.js +9 -0
- package/dist/app/javascript/context_drop/markdown.d.ts +7 -0
- package/dist/app/javascript/context_drop/markdown.js +129 -0
- package/dist/app/javascript/context_drop/sanitizer.d.ts +38 -0
- package/dist/app/javascript/context_drop/sanitizer.js +769 -0
- package/dist/app/javascript/context_drop/screenshot.d.ts +21 -0
- package/dist/app/javascript/context_drop/screenshot.js +245 -0
- package/dist/app/javascript/context_drop/styles.d.ts +1 -0
- package/dist/app/javascript/context_drop/styles.js +2015 -0
- package/dist/app/javascript/context_drop/types.d.ts +239 -0
- package/dist/app/javascript/context_drop/types.js +1 -0
- package/dist/app/javascript/context_drop/widget.d.ts +188 -0
- package/dist/app/javascript/context_drop/widget.js +3227 -0
- package/dist/src/cli.d.ts +3 -0
- package/dist/src/cli.js +207 -0
- package/dist/src/index.d.ts +5 -0
- package/dist/src/index.js +5 -0
- package/dist/src/mcp/dispatcher.d.ts +42 -0
- package/dist/src/mcp/dispatcher.js +869 -0
- package/dist/src/mcp/environment-router.d.ts +38 -0
- package/dist/src/mcp/environment-router.js +174 -0
- package/dist/src/mcp/index.d.ts +24 -0
- package/dist/src/mcp/index.js +75 -0
- package/dist/src/mcp/remote-client.d.ts +25 -0
- package/dist/src/mcp/remote-client.js +98 -0
- package/dist/src/next/index.d.ts +13 -0
- package/dist/src/next/index.js +25 -0
- package/dist/src/next/server.d.ts +1 -0
- package/dist/src/next/server.js +1 -0
- package/dist/src/react/index.d.ts +90 -0
- package/dist/src/react/index.js +124 -0
- package/dist/src/server/handler.d.ts +42 -0
- package/dist/src/server/handler.js +677 -0
- package/dist/src/server/index.d.ts +8 -0
- package/dist/src/server/index.js +8 -0
- package/dist/src/server/sanitizer.d.ts +15 -0
- package/dist/src/server/sanitizer.js +201 -0
- package/dist/src/server/storage/local.d.ts +22 -0
- package/dist/src/server/storage/local.js +574 -0
- package/dist/src/server/storage/memory.d.ts +5 -0
- package/dist/src/server/storage/memory.js +221 -0
- package/dist/src/server/storage/postgres.d.ts +27 -0
- package/dist/src/server/storage/postgres.js +446 -0
- package/dist/src/server/types.d.ts +190 -0
- package/dist/src/server/types.js +7 -0
- package/dist/src/server/upload-validator.d.ts +16 -0
- package/dist/src/server/upload-validator.js +196 -0
- package/dist/src/widget/index.d.ts +1 -0
- package/dist/src/widget/index.js +1 -0
- package/package.json +99 -4
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
import type { RemoteMcpEnvironmentConfig } from "../server/types.js";
|
|
2
|
+
export interface NormalizedRemoteEnvConfig {
|
|
3
|
+
url: string;
|
|
4
|
+
apiKey: string;
|
|
5
|
+
headers: Record<string, string>;
|
|
6
|
+
allowInsecureHttp: boolean;
|
|
7
|
+
}
|
|
8
|
+
export interface AvailableMcpEnvironmentInfo {
|
|
9
|
+
name: string;
|
|
10
|
+
aliases: string[];
|
|
11
|
+
mode: "local_db" | "remote_http";
|
|
12
|
+
url?: string;
|
|
13
|
+
has_api_key?: boolean;
|
|
14
|
+
configured: boolean;
|
|
15
|
+
reachable?: boolean;
|
|
16
|
+
error?: string;
|
|
17
|
+
}
|
|
18
|
+
export type EnvVarMap = Record<string, string | undefined>;
|
|
19
|
+
export declare function resolveHostEnvironment(explicit?: string, envVars?: EnvVarMap): string;
|
|
20
|
+
export declare function resolveEffectiveMcpEnvironments(explicitMap?: Record<string, RemoteMcpEnvironmentConfig>, envVars?: EnvVarMap): Record<string, NormalizedRemoteEnvConfig>;
|
|
21
|
+
export declare function redactEndpointUrl(urlStr: string): string;
|
|
22
|
+
export declare class EnvironmentRouter {
|
|
23
|
+
readonly hostEnvironment: string;
|
|
24
|
+
readonly localOnly: boolean;
|
|
25
|
+
private readonly remotes;
|
|
26
|
+
constructor(options?: {
|
|
27
|
+
hostEnvironment?: string;
|
|
28
|
+
environments?: Record<string, RemoteMcpEnvironmentConfig>;
|
|
29
|
+
localOnly?: boolean;
|
|
30
|
+
envVars?: EnvVarMap;
|
|
31
|
+
});
|
|
32
|
+
getRemoteEnvironments(): Record<string, NormalizedRemoteEnvConfig>;
|
|
33
|
+
getRemoteConfig(envName: string | null | undefined): NormalizedRemoteEnvConfig | null;
|
|
34
|
+
getAvailableEnvironments(): AvailableMcpEnvironmentInfo[];
|
|
35
|
+
parseTarget(rawId: string | number, explicitEnvironment?: string | null): [string | null, string];
|
|
36
|
+
isLocalTarget(envName: string | null | undefined): boolean;
|
|
37
|
+
qualifyId(envName: string, id: string | number): string;
|
|
38
|
+
}
|
|
@@ -0,0 +1,174 @@
|
|
|
1
|
+
export function resolveHostEnvironment(explicit, envVars = process.env) {
|
|
2
|
+
const trimmed = (explicit ?? "").trim().toLowerCase();
|
|
3
|
+
if (trimmed.length > 0)
|
|
4
|
+
return trimmed;
|
|
5
|
+
const fromEnv = (envVars["CONTEXT_DROP_ENV"] ||
|
|
6
|
+
envVars["VERCEL_ENV"] ||
|
|
7
|
+
envVars["NEXT_PUBLIC_VERCEL_ENV"] ||
|
|
8
|
+
envVars["RAILS_ENV"] ||
|
|
9
|
+
envVars["NODE_ENV"] ||
|
|
10
|
+
"development")
|
|
11
|
+
.trim()
|
|
12
|
+
.toLowerCase();
|
|
13
|
+
return fromEnv || "development";
|
|
14
|
+
}
|
|
15
|
+
function normalizeRemoteEntry(name, rawCfg) {
|
|
16
|
+
const envName = name.trim().toLowerCase();
|
|
17
|
+
if (!envName || envName === "local" || envName === "all")
|
|
18
|
+
return null;
|
|
19
|
+
if (!rawCfg || typeof rawCfg !== "object" || Array.isArray(rawCfg))
|
|
20
|
+
return null;
|
|
21
|
+
const rec = rawCfg;
|
|
22
|
+
const url = String(rec["url"] ?? "").trim();
|
|
23
|
+
if (!url)
|
|
24
|
+
return null;
|
|
25
|
+
const rawHeaders = rec["headers"];
|
|
26
|
+
const headers = {};
|
|
27
|
+
if (rawHeaders && typeof rawHeaders === "object" && !Array.isArray(rawHeaders)) {
|
|
28
|
+
for (const [k, v] of Object.entries(rawHeaders)) {
|
|
29
|
+
if (k.trim()) {
|
|
30
|
+
headers[k.trim()] = String(v ?? "");
|
|
31
|
+
}
|
|
32
|
+
}
|
|
33
|
+
}
|
|
34
|
+
const apiKey = String(rec["apiKey"] ?? rec["api_key"] ?? "").trim();
|
|
35
|
+
const allowInsecureHttp = Boolean(rec["allowInsecureHttp"] ?? rec["allow_insecure_http"] ?? false);
|
|
36
|
+
return {
|
|
37
|
+
name: envName,
|
|
38
|
+
config: {
|
|
39
|
+
url,
|
|
40
|
+
apiKey,
|
|
41
|
+
headers,
|
|
42
|
+
allowInsecureHttp
|
|
43
|
+
}
|
|
44
|
+
};
|
|
45
|
+
}
|
|
46
|
+
export function resolveEffectiveMcpEnvironments(explicitMap, envVars = process.env) {
|
|
47
|
+
const merged = {};
|
|
48
|
+
const rawJson = (envVars["CONTEXT_DROP_REMOTE_ENVIRONMENTS"] ?? "").trim();
|
|
49
|
+
if (rawJson) {
|
|
50
|
+
try {
|
|
51
|
+
const parsed = JSON.parse(rawJson);
|
|
52
|
+
if (parsed && typeof parsed === "object" && !Array.isArray(parsed)) {
|
|
53
|
+
for (const [name, cfg] of Object.entries(parsed)) {
|
|
54
|
+
const norm = normalizeRemoteEntry(name, cfg);
|
|
55
|
+
if (norm)
|
|
56
|
+
merged[norm.name] = norm.config;
|
|
57
|
+
}
|
|
58
|
+
}
|
|
59
|
+
}
|
|
60
|
+
catch {
|
|
61
|
+
// Ignore malformed JSON env variable
|
|
62
|
+
}
|
|
63
|
+
}
|
|
64
|
+
for (const [key, val] of Object.entries(envVars)) {
|
|
65
|
+
const match = /^CONTEXT_DROP_([A-Z0-9_]+)_MCP_URL$/.exec(key);
|
|
66
|
+
if (!match)
|
|
67
|
+
continue;
|
|
68
|
+
const tokenKey = match[1];
|
|
69
|
+
if (!tokenKey)
|
|
70
|
+
continue;
|
|
71
|
+
const urlStr = (val ?? "").trim();
|
|
72
|
+
if (!urlStr)
|
|
73
|
+
continue;
|
|
74
|
+
const envName = tokenKey.toLowerCase();
|
|
75
|
+
const apiKey = (envVars[`CONTEXT_DROP_${tokenKey}_MCP_API_KEY`] ?? "").trim();
|
|
76
|
+
const allowInsecure = (envVars[`CONTEXT_DROP_${tokenKey}_MCP_ALLOW_INSECURE_HTTP`] ?? "").trim() === "true";
|
|
77
|
+
const norm = normalizeRemoteEntry(envName, {
|
|
78
|
+
url: urlStr,
|
|
79
|
+
apiKey,
|
|
80
|
+
allowInsecureHttp: allowInsecure
|
|
81
|
+
});
|
|
82
|
+
if (norm)
|
|
83
|
+
merged[norm.name] = norm.config;
|
|
84
|
+
}
|
|
85
|
+
if (explicitMap) {
|
|
86
|
+
for (const [name, cfg] of Object.entries(explicitMap)) {
|
|
87
|
+
const norm = normalizeRemoteEntry(name, cfg);
|
|
88
|
+
if (norm)
|
|
89
|
+
merged[norm.name] = norm.config;
|
|
90
|
+
}
|
|
91
|
+
}
|
|
92
|
+
return merged;
|
|
93
|
+
}
|
|
94
|
+
export function redactEndpointUrl(urlStr) {
|
|
95
|
+
try {
|
|
96
|
+
const parsed = new URL(urlStr);
|
|
97
|
+
const portSuffix = parsed.port && parsed.port !== "80" && parsed.port !== "443" ? `:${parsed.port}` : "";
|
|
98
|
+
return `${parsed.protocol}//${parsed.hostname}${portSuffix}${parsed.pathname}`;
|
|
99
|
+
}
|
|
100
|
+
catch {
|
|
101
|
+
return urlStr;
|
|
102
|
+
}
|
|
103
|
+
}
|
|
104
|
+
export class EnvironmentRouter {
|
|
105
|
+
hostEnvironment;
|
|
106
|
+
localOnly;
|
|
107
|
+
remotes;
|
|
108
|
+
constructor(options = {}) {
|
|
109
|
+
const envVars = options.envVars ?? process.env;
|
|
110
|
+
this.hostEnvironment = resolveHostEnvironment(options.hostEnvironment, envVars);
|
|
111
|
+
this.localOnly = Boolean(options.localOnly);
|
|
112
|
+
if (this.localOnly) {
|
|
113
|
+
this.remotes = {};
|
|
114
|
+
}
|
|
115
|
+
else {
|
|
116
|
+
const all = resolveEffectiveMcpEnvironments(options.environments, envVars);
|
|
117
|
+
delete all[this.hostEnvironment];
|
|
118
|
+
this.remotes = all;
|
|
119
|
+
}
|
|
120
|
+
}
|
|
121
|
+
getRemoteEnvironments() {
|
|
122
|
+
return { ...this.remotes };
|
|
123
|
+
}
|
|
124
|
+
getRemoteConfig(envName) {
|
|
125
|
+
if (!envName)
|
|
126
|
+
return null;
|
|
127
|
+
return this.remotes[envName.trim().toLowerCase()] ?? null;
|
|
128
|
+
}
|
|
129
|
+
getAvailableEnvironments() {
|
|
130
|
+
const list = [
|
|
131
|
+
{
|
|
132
|
+
name: this.hostEnvironment,
|
|
133
|
+
aliases: this.hostEnvironment === "local" ? [] : ["local"],
|
|
134
|
+
mode: "local_db",
|
|
135
|
+
configured: true
|
|
136
|
+
}
|
|
137
|
+
];
|
|
138
|
+
for (const [name, cfg] of Object.entries(this.remotes)) {
|
|
139
|
+
list.push({
|
|
140
|
+
name,
|
|
141
|
+
aliases: [],
|
|
142
|
+
mode: "remote_http",
|
|
143
|
+
url: redactEndpointUrl(cfg.url),
|
|
144
|
+
has_api_key: cfg.apiKey.length > 0,
|
|
145
|
+
configured: true
|
|
146
|
+
});
|
|
147
|
+
}
|
|
148
|
+
return list;
|
|
149
|
+
}
|
|
150
|
+
parseTarget(rawId, explicitEnvironment) {
|
|
151
|
+
let envStr = (explicitEnvironment ?? "").trim().toLowerCase();
|
|
152
|
+
let idStr = String(rawId).trim();
|
|
153
|
+
const colonIdx = idStr.indexOf(":");
|
|
154
|
+
if (colonIdx > 0 && colonIdx < idStr.length - 1) {
|
|
155
|
+
const prefix = idStr.slice(0, colonIdx).trim().toLowerCase();
|
|
156
|
+
const rest = idStr.slice(colonIdx + 1).trim();
|
|
157
|
+
if (prefix && rest) {
|
|
158
|
+
if (!envStr)
|
|
159
|
+
envStr = prefix;
|
|
160
|
+
idStr = rest;
|
|
161
|
+
}
|
|
162
|
+
}
|
|
163
|
+
return [envStr || null, idStr];
|
|
164
|
+
}
|
|
165
|
+
isLocalTarget(envName) {
|
|
166
|
+
if (!envName || !envName.trim())
|
|
167
|
+
return true;
|
|
168
|
+
const norm = envName.trim().toLowerCase();
|
|
169
|
+
return norm === "local" || norm === this.hostEnvironment;
|
|
170
|
+
}
|
|
171
|
+
qualifyId(envName, id) {
|
|
172
|
+
return `${envName}:${id}`;
|
|
173
|
+
}
|
|
174
|
+
}
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
import type { RemoteMcpEnvironmentConfig, StorageAdapter } from "../server/types.js";
|
|
2
|
+
import { ContextDropMcpDispatcher, MCP_PROTOCOL_VERSION, type ContextDropMcpOptions, type JsonRpcResponse } from "./dispatcher.js";
|
|
3
|
+
export { ContextDropMcpDispatcher, MCP_PROTOCOL_VERSION, type ContextDropMcpOptions, type JsonRpcResponse };
|
|
4
|
+
export { EnvironmentRouter, redactEndpointUrl, resolveEffectiveMcpEnvironments, resolveHostEnvironment, type AvailableMcpEnvironmentInfo, type EnvVarMap, type NormalizedRemoteEnvConfig } from "./environment-router.js";
|
|
5
|
+
export { RemoteMcpClient, RemoteMcpError, validateRemoteMcpUrl, type McpContentBlock, type McpToolResult, type RemoteMcpTransport } from "./remote-client.js";
|
|
6
|
+
export declare function createContextDropMcpServer(options?: Partial<ContextDropMcpOptions>): {
|
|
7
|
+
dispatcher: ContextDropMcpDispatcher;
|
|
8
|
+
handleHttpPayload: (payload: unknown, httpOptions?: {
|
|
9
|
+
localOnly?: boolean;
|
|
10
|
+
}) => Promise<JsonRpcResponse[] | JsonRpcResponse | null>;
|
|
11
|
+
};
|
|
12
|
+
export interface StdioWritableLike {
|
|
13
|
+
write(chunk: string): unknown;
|
|
14
|
+
}
|
|
15
|
+
export interface StdioMcpRunnerOptions {
|
|
16
|
+
storage?: StorageAdapter;
|
|
17
|
+
storageDir?: string;
|
|
18
|
+
hostEnvironment?: string;
|
|
19
|
+
environments?: Record<string, RemoteMcpEnvironmentConfig>;
|
|
20
|
+
input?: AsyncIterable<Uint8Array | string>;
|
|
21
|
+
output?: StdioWritableLike;
|
|
22
|
+
error?: StdioWritableLike;
|
|
23
|
+
}
|
|
24
|
+
export declare function runContextDropMcpStdio(options?: StdioMcpRunnerOptions): Promise<void>;
|
|
@@ -0,0 +1,75 @@
|
|
|
1
|
+
import readline from "node:readline";
|
|
2
|
+
import { createLocalStorageAdapter } from "../server/storage/local.js";
|
|
3
|
+
import { ContextDropMcpDispatcher, MCP_PROTOCOL_VERSION } from "./dispatcher.js";
|
|
4
|
+
export { ContextDropMcpDispatcher, MCP_PROTOCOL_VERSION };
|
|
5
|
+
export { EnvironmentRouter, redactEndpointUrl, resolveEffectiveMcpEnvironments, resolveHostEnvironment } from "./environment-router.js";
|
|
6
|
+
export { RemoteMcpClient, RemoteMcpError, validateRemoteMcpUrl } from "./remote-client.js";
|
|
7
|
+
export function createContextDropMcpServer(options = {}) {
|
|
8
|
+
const storage = options.storage ??
|
|
9
|
+
createLocalStorageAdapter({
|
|
10
|
+
dir: process.env.CONTEXT_DROP_STORAGE_DIR
|
|
11
|
+
});
|
|
12
|
+
const dispatcher = new ContextDropMcpDispatcher({
|
|
13
|
+
...options,
|
|
14
|
+
storage
|
|
15
|
+
});
|
|
16
|
+
return {
|
|
17
|
+
dispatcher,
|
|
18
|
+
handleHttpPayload: async (payload, httpOptions = { localOnly: true }) => {
|
|
19
|
+
if (httpOptions.localOnly !== false) {
|
|
20
|
+
const localOnlyDispatcher = new ContextDropMcpDispatcher({
|
|
21
|
+
...options,
|
|
22
|
+
storage,
|
|
23
|
+
localOnly: true
|
|
24
|
+
});
|
|
25
|
+
return localOnlyDispatcher.dispatch(payload);
|
|
26
|
+
}
|
|
27
|
+
return dispatcher.dispatch(payload);
|
|
28
|
+
}
|
|
29
|
+
};
|
|
30
|
+
}
|
|
31
|
+
export async function runContextDropMcpStdio(options = {}) {
|
|
32
|
+
const input = (options.input ?? process.stdin);
|
|
33
|
+
const output = options.output ?? process.stdout;
|
|
34
|
+
const errorOut = options.error ?? process.stderr;
|
|
35
|
+
const storage = options.storage ??
|
|
36
|
+
createLocalStorageAdapter({
|
|
37
|
+
dir: options.storageDir || process.env.CONTEXT_DROP_STORAGE_DIR
|
|
38
|
+
});
|
|
39
|
+
const dispatcher = new ContextDropMcpDispatcher({
|
|
40
|
+
storage,
|
|
41
|
+
hostEnvironment: options.hostEnvironment,
|
|
42
|
+
environments: options.environments,
|
|
43
|
+
localOnly: false
|
|
44
|
+
});
|
|
45
|
+
const rl = readline.createInterface({
|
|
46
|
+
input,
|
|
47
|
+
crlfDelay: Infinity
|
|
48
|
+
});
|
|
49
|
+
for await (const line of rl) {
|
|
50
|
+
const stripped = line.trim();
|
|
51
|
+
if (!stripped)
|
|
52
|
+
continue;
|
|
53
|
+
let response = null;
|
|
54
|
+
try {
|
|
55
|
+
const parsed = JSON.parse(stripped);
|
|
56
|
+
response = await dispatcher.dispatch(parsed);
|
|
57
|
+
}
|
|
58
|
+
catch (err) {
|
|
59
|
+
response = {
|
|
60
|
+
jsonrpc: "2.0",
|
|
61
|
+
id: null,
|
|
62
|
+
error: {
|
|
63
|
+
code: -32700,
|
|
64
|
+
message: `Parse error: ${err.message}`
|
|
65
|
+
}
|
|
66
|
+
};
|
|
67
|
+
}
|
|
68
|
+
if (response !== null) {
|
|
69
|
+
output.write(`${JSON.stringify(response)}\n`);
|
|
70
|
+
}
|
|
71
|
+
}
|
|
72
|
+
if (process.env.CONTEXT_DROP_MCP_DEBUG === "1") {
|
|
73
|
+
errorOut.write("[context-drop] MCP stdio session closed.\n");
|
|
74
|
+
}
|
|
75
|
+
}
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
import type { NormalizedRemoteEnvConfig } from "./environment-router.js";
|
|
2
|
+
export interface McpContentBlock {
|
|
3
|
+
type: string;
|
|
4
|
+
text?: string;
|
|
5
|
+
data?: string;
|
|
6
|
+
mimeType?: string;
|
|
7
|
+
[key: string]: unknown;
|
|
8
|
+
}
|
|
9
|
+
export interface McpToolResult {
|
|
10
|
+
content: McpContentBlock[];
|
|
11
|
+
isError: boolean;
|
|
12
|
+
}
|
|
13
|
+
export type RemoteMcpTransport = (envName: string, envConfig: NormalizedRemoteEnvConfig, method: string, params: Record<string, unknown>) => Promise<unknown>;
|
|
14
|
+
export declare class RemoteMcpError extends Error {
|
|
15
|
+
constructor(message: string);
|
|
16
|
+
}
|
|
17
|
+
export declare function validateRemoteMcpUrl(envName: string, urlStr: string, allowInsecureHttp?: boolean): URL;
|
|
18
|
+
export declare class RemoteMcpClient {
|
|
19
|
+
private readonly transport?;
|
|
20
|
+
constructor(options?: {
|
|
21
|
+
transport?: RemoteMcpTransport;
|
|
22
|
+
});
|
|
23
|
+
callRpc(envName: string, envConfig: NormalizedRemoteEnvConfig, method: string, params?: Record<string, unknown>): Promise<unknown>;
|
|
24
|
+
callTool(envName: string, envConfig: NormalizedRemoteEnvConfig, toolName: string, args?: Record<string, unknown>): Promise<McpToolResult>;
|
|
25
|
+
}
|
|
@@ -0,0 +1,98 @@
|
|
|
1
|
+
const LOOPBACK_HOSTS = new Set(["localhost", "127.0.0.1", "::1", "[::1]"]);
|
|
2
|
+
const DEFAULT_TIMEOUT_MS = 12_000;
|
|
3
|
+
export class RemoteMcpError extends Error {
|
|
4
|
+
constructor(message) {
|
|
5
|
+
super(message);
|
|
6
|
+
this.name = "RemoteMcpError";
|
|
7
|
+
}
|
|
8
|
+
}
|
|
9
|
+
export function validateRemoteMcpUrl(envName, urlStr, allowInsecureHttp = false) {
|
|
10
|
+
let parsed;
|
|
11
|
+
try {
|
|
12
|
+
parsed = new URL(urlStr);
|
|
13
|
+
}
|
|
14
|
+
catch (err) {
|
|
15
|
+
throw new RemoteMcpError(`Malformed MCP URL for environment '${envName}': ${err.message}`);
|
|
16
|
+
}
|
|
17
|
+
if (parsed.protocol !== "http:" && parsed.protocol !== "https:") {
|
|
18
|
+
throw new RemoteMcpError(`Invalid MCP URL for environment '${envName}': ${urlStr}`);
|
|
19
|
+
}
|
|
20
|
+
const isLoopback = LOOPBACK_HOSTS.has(parsed.hostname.toLowerCase());
|
|
21
|
+
if (parsed.protocol === "http:" && !isLoopback && !allowInsecureHttp) {
|
|
22
|
+
throw new RemoteMcpError(`Insecure HTTP MCP URL refused for remote environment '${envName}' (use https:// or set allowInsecureHttp: true)`);
|
|
23
|
+
}
|
|
24
|
+
return parsed;
|
|
25
|
+
}
|
|
26
|
+
export class RemoteMcpClient {
|
|
27
|
+
transport;
|
|
28
|
+
constructor(options = {}) {
|
|
29
|
+
this.transport = options.transport;
|
|
30
|
+
}
|
|
31
|
+
async callRpc(envName, envConfig, method, params = {}) {
|
|
32
|
+
if (this.transport) {
|
|
33
|
+
return this.transport(envName, envConfig, method, params);
|
|
34
|
+
}
|
|
35
|
+
const parsedUrl = validateRemoteMcpUrl(envName, envConfig.url, envConfig.allowInsecureHttp);
|
|
36
|
+
const payload = {
|
|
37
|
+
jsonrpc: "2.0",
|
|
38
|
+
id: `fed_${envName}_${Date.now()}`,
|
|
39
|
+
method,
|
|
40
|
+
params
|
|
41
|
+
};
|
|
42
|
+
const headers = {
|
|
43
|
+
"Content-Type": "application/json",
|
|
44
|
+
Accept: "application/json",
|
|
45
|
+
"X-Context-Drop-Federated": "1",
|
|
46
|
+
...envConfig.headers
|
|
47
|
+
};
|
|
48
|
+
if (envConfig.apiKey) {
|
|
49
|
+
headers["Authorization"] = `Bearer ${envConfig.apiKey}`;
|
|
50
|
+
}
|
|
51
|
+
let response;
|
|
52
|
+
try {
|
|
53
|
+
response = await fetch(parsedUrl.toString(), {
|
|
54
|
+
method: "POST",
|
|
55
|
+
headers,
|
|
56
|
+
body: JSON.stringify(payload),
|
|
57
|
+
signal: AbortSignal.timeout(DEFAULT_TIMEOUT_MS)
|
|
58
|
+
});
|
|
59
|
+
}
|
|
60
|
+
catch (err) {
|
|
61
|
+
throw new RemoteMcpError(`Failed to reach remote environment '${envName}': ${err.message}`);
|
|
62
|
+
}
|
|
63
|
+
if (!response.ok) {
|
|
64
|
+
throw new RemoteMcpError(`Remote environment '${envName}' returned HTTP ${response.status}`);
|
|
65
|
+
}
|
|
66
|
+
let json;
|
|
67
|
+
try {
|
|
68
|
+
json = (await response.json());
|
|
69
|
+
}
|
|
70
|
+
catch (err) {
|
|
71
|
+
throw new RemoteMcpError(`Invalid JSON from remote environment '${envName}': ${err.message}`);
|
|
72
|
+
}
|
|
73
|
+
if (json && typeof json["error"] === "object" && json["error"] !== null) {
|
|
74
|
+
const errObj = json["error"];
|
|
75
|
+
throw new RemoteMcpError(`Remote environment '${envName}' error: ${String(errObj["message"] ?? "Unknown error")}`);
|
|
76
|
+
}
|
|
77
|
+
return json ? json["result"] : null;
|
|
78
|
+
}
|
|
79
|
+
async callTool(envName, envConfig, toolName, args = {}) {
|
|
80
|
+
const cleanArgs = { ...args };
|
|
81
|
+
delete cleanArgs["environment"];
|
|
82
|
+
const rawResult = await this.callRpc(envName, envConfig, "tools/call", {
|
|
83
|
+
name: toolName,
|
|
84
|
+
arguments: cleanArgs
|
|
85
|
+
});
|
|
86
|
+
if (!rawResult || typeof rawResult !== "object") {
|
|
87
|
+
throw new RemoteMcpError(`Unexpected tool response from '${envName}'`);
|
|
88
|
+
}
|
|
89
|
+
const resObj = rawResult;
|
|
90
|
+
const content = Array.isArray(resObj["content"])
|
|
91
|
+
? resObj["content"]
|
|
92
|
+
: [];
|
|
93
|
+
return {
|
|
94
|
+
content,
|
|
95
|
+
isError: Boolean(resObj["isError"])
|
|
96
|
+
};
|
|
97
|
+
}
|
|
98
|
+
}
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import * as React from "react";
|
|
2
|
+
import { useContextDrop, type ContextDropHandle, type ContextDropProps } from "../react/index.js";
|
|
3
|
+
export type { ContextDropHandle, ContextDropProps, FeedbackSubmissionPayload, FeedbackSubmissionResponse, SelectionMode, WidgetConfig, WidgetPosition } from "../react/index.js";
|
|
4
|
+
export { useContextDrop };
|
|
5
|
+
/**
|
|
6
|
+
* Drop-in Next.js Client Component for App Router (`app/layout.tsx`) and Pages Router (`pages/_app.tsx`).
|
|
7
|
+
*
|
|
8
|
+
* Defaults `endpoint` to `"/api/context-drop"` (matching `createContextDropNextHandler()`).
|
|
9
|
+
* Pass `endpoint="/context_drop"` or `endpoint="https://api.example.com/context_drop"` when connecting
|
|
10
|
+
* to a Ruby on Rails ContextDrop backend.
|
|
11
|
+
*/
|
|
12
|
+
export declare const ContextDrop: React.ForwardRefExoticComponent<ContextDropProps & React.RefAttributes<ContextDropHandle>>;
|
|
13
|
+
export declare const ContextDropWidget: React.ForwardRefExoticComponent<ContextDropProps & React.RefAttributes<ContextDropHandle>>;
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
"use client";
|
|
2
|
+
import * as React from "react";
|
|
3
|
+
import { ContextDrop as ReactContextDrop, useContextDrop } from "../react/index.js";
|
|
4
|
+
export { useContextDrop };
|
|
5
|
+
/**
|
|
6
|
+
* Drop-in Next.js Client Component for App Router (`app/layout.tsx`) and Pages Router (`pages/_app.tsx`).
|
|
7
|
+
*
|
|
8
|
+
* Defaults `endpoint` to `"/api/context-drop"` (matching `createContextDropNextHandler()`).
|
|
9
|
+
* Pass `endpoint="/context_drop"` or `endpoint="https://api.example.com/context_drop"` when connecting
|
|
10
|
+
* to a Ruby on Rails ContextDrop backend.
|
|
11
|
+
*/
|
|
12
|
+
export const ContextDrop = React.forwardRef(function NextContextDrop(props, ref) {
|
|
13
|
+
const { endpoint = "/api/context-drop", metadata, ...rest } = props;
|
|
14
|
+
const mergedMetadata = React.useMemo(() => ({
|
|
15
|
+
framework: "nextjs",
|
|
16
|
+
...(metadata ?? {})
|
|
17
|
+
}), [metadata]);
|
|
18
|
+
return React.createElement(ReactContextDrop, {
|
|
19
|
+
...rest,
|
|
20
|
+
ref,
|
|
21
|
+
endpoint,
|
|
22
|
+
metadata: mergedMetadata
|
|
23
|
+
});
|
|
24
|
+
});
|
|
25
|
+
export const ContextDropWidget = ContextDrop;
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * from "../server/index.js";
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * from "../server/index.js";
|
|
@@ -0,0 +1,90 @@
|
|
|
1
|
+
import * as React from "react";
|
|
2
|
+
import type { FeedbackSubmissionPayload, FeedbackSubmissionResponse, SelectionMode, WidgetConfig, WidgetPosition } from "../../app/javascript/context_drop/types.js";
|
|
3
|
+
import { ContextDropWidgetElement, registerContextDropWidget } from "../../app/javascript/context_drop/widget.js";
|
|
4
|
+
export type { FeedbackSubmissionPayload, FeedbackSubmissionResponse, SelectionMode, WidgetConfig, WidgetPosition };
|
|
5
|
+
export { ContextDropWidgetElement, registerContextDropWidget };
|
|
6
|
+
export interface ContextDropHandle {
|
|
7
|
+
open: () => void;
|
|
8
|
+
close: () => void;
|
|
9
|
+
toggle: () => void;
|
|
10
|
+
element: ContextDropWidgetElement | null;
|
|
11
|
+
}
|
|
12
|
+
export type ReactWidgetPosition = WidgetPosition | "bottom-right" | "bottom-left" | "top-right" | "top-left";
|
|
13
|
+
export interface ContextDropProps {
|
|
14
|
+
/**
|
|
15
|
+
* Base URL for the ContextDrop backend API.
|
|
16
|
+
* - Next.js standalone route handler: `"/api/context-drop"`
|
|
17
|
+
* - Rails Engine (same-origin or proxied): `"/context_drop"`
|
|
18
|
+
* - Cross-origin Rails / Next.js server: `"https://api.example.com/context_drop"`
|
|
19
|
+
*/
|
|
20
|
+
endpoint?: string;
|
|
21
|
+
/**
|
|
22
|
+
* Viewport corner where the folded launcher rests (accepts both `"bottom_right"` and `"bottom-right"`).
|
|
23
|
+
*/
|
|
24
|
+
position?: ReactWidgetPosition;
|
|
25
|
+
/**
|
|
26
|
+
* Custom composer textarea placeholder text.
|
|
27
|
+
*/
|
|
28
|
+
placeholder?: string;
|
|
29
|
+
/**
|
|
30
|
+
* Scoped widget color theme (`"light"` or `"dark"`). Never alters host page styles.
|
|
31
|
+
*/
|
|
32
|
+
theme?: "light" | "dark";
|
|
33
|
+
/**
|
|
34
|
+
* Override element picker mode (`"smart"` for non-technical end users, `"verbose"` for QA/devs).
|
|
35
|
+
*/
|
|
36
|
+
selectionMode?: SelectionMode;
|
|
37
|
+
/**
|
|
38
|
+
* Environment stamp (`"development"`, `"staging"`, `"production"`, etc.).
|
|
39
|
+
*/
|
|
40
|
+
environment?: string;
|
|
41
|
+
/**
|
|
42
|
+
* Custom JSON-serializable key-value metadata attached to every submission (e.g. `userId`, `plan`, `release`).
|
|
43
|
+
*/
|
|
44
|
+
metadata?: Record<string, unknown>;
|
|
45
|
+
/**
|
|
46
|
+
* Optional inline `WidgetConfig` overrides. When provided, skips the initial `GET /config` request.
|
|
47
|
+
*/
|
|
48
|
+
config?: Partial<WidgetConfig>;
|
|
49
|
+
/**
|
|
50
|
+
* Fetch credentials mode (`"same-origin"`, `"include"`, or `"omit"`).
|
|
51
|
+
*/
|
|
52
|
+
credentials?: RequestCredentials;
|
|
53
|
+
/**
|
|
54
|
+
* Custom HTTP headers sent on ContextDrop API requests (e.g. `Authorization` or tenant headers).
|
|
55
|
+
*/
|
|
56
|
+
headers?: Record<string, string>;
|
|
57
|
+
/**
|
|
58
|
+
* When `true`, unmounts and disables the widget completely.
|
|
59
|
+
*/
|
|
60
|
+
disabled?: boolean;
|
|
61
|
+
/**
|
|
62
|
+
* Fired after a feedback submission succeeds.
|
|
63
|
+
*/
|
|
64
|
+
onSubmitted?: (event: {
|
|
65
|
+
response: FeedbackSubmissionResponse;
|
|
66
|
+
payload: FeedbackSubmissionPayload;
|
|
67
|
+
}) => void;
|
|
68
|
+
/**
|
|
69
|
+
* Fired when a feedback submission fails.
|
|
70
|
+
*/
|
|
71
|
+
onError?: (event: {
|
|
72
|
+
error: string;
|
|
73
|
+
}) => void;
|
|
74
|
+
/**
|
|
75
|
+
* Fired when the feedback composer opens or closes.
|
|
76
|
+
*/
|
|
77
|
+
onOpenChange?: (open: boolean) => void;
|
|
78
|
+
}
|
|
79
|
+
export declare const ContextDrop: React.ForwardRefExoticComponent<ContextDropProps & React.RefAttributes<ContextDropHandle>>;
|
|
80
|
+
export declare const ContextDropWidget: React.ForwardRefExoticComponent<ContextDropProps & React.RefAttributes<ContextDropHandle>>;
|
|
81
|
+
/**
|
|
82
|
+
* Imperative helper hook to open, close, or toggle the mounted `<ContextDrop />` widget
|
|
83
|
+
* from any button or menu item in your React/Next.js tree without requiring a Context Provider.
|
|
84
|
+
*/
|
|
85
|
+
export declare function useContextDrop(): {
|
|
86
|
+
open: () => void;
|
|
87
|
+
close: () => void;
|
|
88
|
+
toggle: () => void;
|
|
89
|
+
getElement: () => ContextDropWidgetElement | null;
|
|
90
|
+
};
|