context-drop 0.0.0-stage → 0.3.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 +710 -2
- package/app/assets/javascripts/context_drop/context_drop.js +9949 -0
- package/dist/app/javascript/context_drop/api-client.d.ts +53 -0
- package/dist/app/javascript/context_drop/api-client.js +470 -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 +34 -0
- package/dist/app/javascript/context_drop/icons.js +34 -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 +2196 -0
- package/dist/app/javascript/context_drop/types.d.ts +316 -0
- package/dist/app/javascript/context_drop/types.js +1 -0
- package/dist/app/javascript/context_drop/widget.d.ts +206 -0
- package/dist/app/javascript/context_drop/widget.js +3752 -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 +895 -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 +104 -0
- package/dist/src/react/index.js +144 -0
- package/dist/src/server/handler.d.ts +48 -0
- package/dist/src/server/handler.js +1180 -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 +827 -0
- package/dist/src/server/storage/memory.d.ts +8 -0
- package/dist/src/server/storage/memory.js +387 -0
- package/dist/src/server/storage/postgres.d.ts +27 -0
- package/dist/src/server/storage/postgres.js +629 -0
- package/dist/src/server/types.d.ts +227 -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
package/dist/src/cli.js
ADDED
|
@@ -0,0 +1,207 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
import fs from "node:fs";
|
|
3
|
+
import { createRequire } from "node:module";
|
|
4
|
+
import path from "node:path";
|
|
5
|
+
import { runContextDropMcpStdio } from "./mcp/index.js";
|
|
6
|
+
import { createLocalStorageAdapter } from "./server/storage/local.js";
|
|
7
|
+
import { createPostgresStorageAdapter } from "./server/storage/postgres.js";
|
|
8
|
+
import { VERSION } from "./server/types.js";
|
|
9
|
+
export function loadEnvFilesIfPresent(cwd = process.cwd()) {
|
|
10
|
+
for (const filename of [".env.local", ".env"]) {
|
|
11
|
+
const filePath = path.join(cwd, filename);
|
|
12
|
+
if (!fs.existsSync(filePath))
|
|
13
|
+
continue;
|
|
14
|
+
try {
|
|
15
|
+
const lines = fs.readFileSync(filePath, "utf8").split(/\r?\n/);
|
|
16
|
+
for (const rawLine of lines) {
|
|
17
|
+
const trimmed = rawLine.trim();
|
|
18
|
+
if (!trimmed || trimmed.startsWith("#"))
|
|
19
|
+
continue;
|
|
20
|
+
const cleaned = trimmed.startsWith("export ") ? trimmed.slice(7).trim() : trimmed;
|
|
21
|
+
const eqIdx = cleaned.indexOf("=");
|
|
22
|
+
if (eqIdx <= 0)
|
|
23
|
+
continue;
|
|
24
|
+
const key = cleaned.slice(0, eqIdx).trim();
|
|
25
|
+
let val = cleaned.slice(eqIdx + 1).trim();
|
|
26
|
+
if ((val.startsWith('"') && val.endsWith('"')) ||
|
|
27
|
+
(val.startsWith("'") && val.endsWith("'"))) {
|
|
28
|
+
val = val.slice(1, -1);
|
|
29
|
+
}
|
|
30
|
+
if (key && process.env[key] === undefined) {
|
|
31
|
+
process.env[key] = val;
|
|
32
|
+
}
|
|
33
|
+
}
|
|
34
|
+
}
|
|
35
|
+
catch {
|
|
36
|
+
// Non-fatal
|
|
37
|
+
}
|
|
38
|
+
}
|
|
39
|
+
}
|
|
40
|
+
function getFlagValue(args, flag) {
|
|
41
|
+
const idx = args.indexOf(flag);
|
|
42
|
+
if (idx !== -1 && idx + 1 < args.length) {
|
|
43
|
+
return args[idx + 1];
|
|
44
|
+
}
|
|
45
|
+
const prefix = `${flag}=`;
|
|
46
|
+
const eqArg = args.find((a) => a.startsWith(prefix));
|
|
47
|
+
return eqArg ? eqArg.slice(prefix.length) : undefined;
|
|
48
|
+
}
|
|
49
|
+
async function resolveCliStorage(args) {
|
|
50
|
+
const pgUrl = getFlagValue(args, "--postgres") ||
|
|
51
|
+
process.env.CONTEXT_DROP_DATABASE_URL ||
|
|
52
|
+
undefined;
|
|
53
|
+
if (pgUrl) {
|
|
54
|
+
try {
|
|
55
|
+
const hostRequire = createRequire(path.join(process.cwd(), "package.json"));
|
|
56
|
+
const pgMod = hostRequire("pg");
|
|
57
|
+
const pool = new pgMod.Pool({ connectionString: pgUrl });
|
|
58
|
+
return createPostgresStorageAdapter({ pool });
|
|
59
|
+
}
|
|
60
|
+
catch (err) {
|
|
61
|
+
process.stderr.write(`[context-drop] Failed to load 'pg' for --postgres / CONTEXT_DROP_DATABASE_URL: ${err.message}\n`);
|
|
62
|
+
process.exit(1);
|
|
63
|
+
}
|
|
64
|
+
}
|
|
65
|
+
const storageDir = getFlagValue(args, "--dir") || process.env.CONTEXT_DROP_STORAGE_DIR;
|
|
66
|
+
return createLocalStorageAdapter({ dir: storageDir });
|
|
67
|
+
}
|
|
68
|
+
function runInitCommand(args, cwd = process.cwd()) {
|
|
69
|
+
const force = args.includes("--force");
|
|
70
|
+
const routerOverride = getFlagValue(args, "--router");
|
|
71
|
+
const hasSrcApp = fs.existsSync(path.join(cwd, "src", "app"));
|
|
72
|
+
const hasApp = fs.existsSync(path.join(cwd, "app"));
|
|
73
|
+
const hasSrcPages = fs.existsSync(path.join(cwd, "src", "pages"));
|
|
74
|
+
const hasPages = fs.existsSync(path.join(cwd, "pages"));
|
|
75
|
+
const usePagesRouter = routerOverride === "pages" ||
|
|
76
|
+
(routerOverride !== "app" && !hasSrcApp && !hasApp && (hasSrcPages || hasPages));
|
|
77
|
+
const isTypeScript = fs.existsSync(path.join(cwd, "tsconfig.json")) ||
|
|
78
|
+
fs.existsSync(path.join(cwd, "next.config.ts"));
|
|
79
|
+
const ext = isTypeScript ? "ts" : "js";
|
|
80
|
+
if (usePagesRouter) {
|
|
81
|
+
const basePagesDir = hasSrcPages ? path.join(cwd, "src", "pages") : path.join(cwd, "pages");
|
|
82
|
+
const routeFile = path.join(basePagesDir, "api", "context-drop", `[[...path]].${ext}`);
|
|
83
|
+
if (!fs.existsSync(routeFile) || force) {
|
|
84
|
+
fs.mkdirSync(path.dirname(routeFile), { recursive: true });
|
|
85
|
+
fs.writeFileSync(routeFile, [
|
|
86
|
+
'import { createContextDropPagesHandler } from "context-drop/next/server";',
|
|
87
|
+
"",
|
|
88
|
+
"export const config = {",
|
|
89
|
+
" api: {",
|
|
90
|
+
" bodyParser: false",
|
|
91
|
+
" }",
|
|
92
|
+
"};",
|
|
93
|
+
"",
|
|
94
|
+
"export default createContextDropPagesHandler({",
|
|
95
|
+
" mcp: {",
|
|
96
|
+
" enabled: Boolean(process.env.CONTEXT_DROP_MCP_API_KEY),",
|
|
97
|
+
" apiKey: process.env.CONTEXT_DROP_MCP_API_KEY",
|
|
98
|
+
" }",
|
|
99
|
+
"});",
|
|
100
|
+
""
|
|
101
|
+
].join("\n"), "utf8");
|
|
102
|
+
process.stdout.write(`Created ${path.relative(cwd, routeFile)}\n`);
|
|
103
|
+
}
|
|
104
|
+
else {
|
|
105
|
+
process.stdout.write(`Skipped ${path.relative(cwd, routeFile)} (already exists)\n`);
|
|
106
|
+
}
|
|
107
|
+
}
|
|
108
|
+
else {
|
|
109
|
+
const baseAppDir = hasSrcApp ? path.join(cwd, "src", "app") : path.join(cwd, "app");
|
|
110
|
+
const routeFile = path.join(baseAppDir, "api", "context-drop", "[[...path]]", `route.${ext}`);
|
|
111
|
+
if (!fs.existsSync(routeFile) || force) {
|
|
112
|
+
fs.mkdirSync(path.dirname(routeFile), { recursive: true });
|
|
113
|
+
fs.writeFileSync(routeFile, [
|
|
114
|
+
'import { createContextDropNextHandler } from "context-drop/next/server";',
|
|
115
|
+
"",
|
|
116
|
+
"export const { GET, POST, PATCH, PUT, DELETE, OPTIONS } = createContextDropNextHandler({",
|
|
117
|
+
" mcp: {",
|
|
118
|
+
" enabled: Boolean(process.env.CONTEXT_DROP_MCP_API_KEY),",
|
|
119
|
+
" apiKey: process.env.CONTEXT_DROP_MCP_API_KEY",
|
|
120
|
+
" }",
|
|
121
|
+
"});",
|
|
122
|
+
""
|
|
123
|
+
].join("\n"), "utf8");
|
|
124
|
+
process.stdout.write(`Created ${path.relative(cwd, routeFile)}\n`);
|
|
125
|
+
}
|
|
126
|
+
else {
|
|
127
|
+
process.stdout.write(`Skipped ${path.relative(cwd, routeFile)} (already exists)\n`);
|
|
128
|
+
}
|
|
129
|
+
}
|
|
130
|
+
const gitignoreFile = path.join(cwd, ".gitignore");
|
|
131
|
+
if (fs.existsSync(gitignoreFile)) {
|
|
132
|
+
const content = fs.readFileSync(gitignoreFile, "utf8");
|
|
133
|
+
if (!content.includes(".context-drop")) {
|
|
134
|
+
const prefix = content.endsWith("\n") ? "" : "\n";
|
|
135
|
+
fs.appendFileSync(gitignoreFile, `${prefix}.context-drop/\n`, "utf8");
|
|
136
|
+
process.stdout.write("Added .context-drop/ to .gitignore\n");
|
|
137
|
+
}
|
|
138
|
+
}
|
|
139
|
+
process.stdout.write([
|
|
140
|
+
"",
|
|
141
|
+
"Next steps:",
|
|
142
|
+
'1. Mount <ContextDrop /> in your root layout (e.g. app/layout.tsx):',
|
|
143
|
+
' import { ContextDrop } from "context-drop/next";',
|
|
144
|
+
" // Inside <body>:",
|
|
145
|
+
" <ContextDrop />",
|
|
146
|
+
"",
|
|
147
|
+
"2. Add the unified MCP server to your IDE (.mcp.json / Cursor / Claude Code / Antigravity):",
|
|
148
|
+
' { "mcpServers": { "context-drop": { "command": "npx", "args": ["context-drop", "mcp"] } } }',
|
|
149
|
+
""
|
|
150
|
+
].join("\n"));
|
|
151
|
+
}
|
|
152
|
+
function printHelp() {
|
|
153
|
+
process.stdout.write([
|
|
154
|
+
`context-drop v${VERSION} — Self-hosted contextual feedback & unified MCP server`,
|
|
155
|
+
"",
|
|
156
|
+
"Usage:",
|
|
157
|
+
" npx context-drop mcp [options] Start unified stdio MCP server (local + remote federation)",
|
|
158
|
+
" npx context-drop init [options] Scaffold Next.js API route handler & .gitignore",
|
|
159
|
+
"",
|
|
160
|
+
"MCP Options:",
|
|
161
|
+
" --dir <path> Local storage directory (default: .context-drop or CONTEXT_DROP_STORAGE_DIR)",
|
|
162
|
+
" --env <name> Host environment name (default: CONTEXT_DROP_ENV / NODE_ENV / development)",
|
|
163
|
+
" --postgres <url> Connect to PostgreSQL (requires 'pg' installed in host project)",
|
|
164
|
+
"",
|
|
165
|
+
"Init Options:",
|
|
166
|
+
" --router <app|pages> Target Next.js App Router or Pages Router (auto-detected by default)",
|
|
167
|
+
" --force Overwrite existing route handler file",
|
|
168
|
+
""
|
|
169
|
+
].join("\n"));
|
|
170
|
+
}
|
|
171
|
+
export async function runCli(argv = process.argv.slice(2)) {
|
|
172
|
+
const cmd = argv[0];
|
|
173
|
+
if (!cmd || cmd === "--help" || cmd === "-h" || cmd === "help") {
|
|
174
|
+
printHelp();
|
|
175
|
+
return;
|
|
176
|
+
}
|
|
177
|
+
if (cmd === "--version" || cmd === "-v" || cmd === "version") {
|
|
178
|
+
process.stdout.write(`${VERSION}\n`);
|
|
179
|
+
return;
|
|
180
|
+
}
|
|
181
|
+
if (cmd === "init") {
|
|
182
|
+
runInitCommand(argv.slice(1));
|
|
183
|
+
return;
|
|
184
|
+
}
|
|
185
|
+
if (cmd === "mcp") {
|
|
186
|
+
loadEnvFilesIfPresent();
|
|
187
|
+
const rest = argv.slice(1);
|
|
188
|
+
const storage = await resolveCliStorage(rest);
|
|
189
|
+
const hostEnvironment = getFlagValue(rest, "--env");
|
|
190
|
+
await runContextDropMcpStdio({
|
|
191
|
+
storage,
|
|
192
|
+
hostEnvironment
|
|
193
|
+
});
|
|
194
|
+
return;
|
|
195
|
+
}
|
|
196
|
+
process.stderr.write(`Unknown command: ${cmd}\n\n`);
|
|
197
|
+
printHelp();
|
|
198
|
+
process.exit(1);
|
|
199
|
+
}
|
|
200
|
+
const isDirectEntry = process.argv[1] &&
|
|
201
|
+
(process.argv[1].endsWith("/cli.js") ||
|
|
202
|
+
process.argv[1].endsWith("\\cli.js") ||
|
|
203
|
+
process.argv[1].endsWith("/cli.ts") ||
|
|
204
|
+
process.argv[1].endsWith("/context-drop"));
|
|
205
|
+
if (isDirectEntry) {
|
|
206
|
+
void runCli();
|
|
207
|
+
}
|
|
@@ -0,0 +1,5 @@
|
|
|
1
|
+
export * from "../app/javascript/context_drop/types.js";
|
|
2
|
+
export { MANDATORY_EXCLUDED_SELECTORS, captureElementSnapshot, computeDomPath, computeSelector, describeSmartComponent, isElementExcluded, resolveCandidateStack, resolveSmartComponent } from "../app/javascript/context_drop/sanitizer.js";
|
|
3
|
+
export { ContextDropApiClient } from "../app/javascript/context_drop/api-client.js";
|
|
4
|
+
export { ElementPicker } from "../app/javascript/context_drop/element-picker.js";
|
|
5
|
+
export { ContextDropWidgetElement, DEFAULT_SIDEBAR_WIDTH, registerContextDropWidget } from "../app/javascript/context_drop/widget.js";
|
|
@@ -0,0 +1,5 @@
|
|
|
1
|
+
export * from "../app/javascript/context_drop/types.js";
|
|
2
|
+
export { MANDATORY_EXCLUDED_SELECTORS, captureElementSnapshot, computeDomPath, computeSelector, describeSmartComponent, isElementExcluded, resolveCandidateStack, resolveSmartComponent } from "../app/javascript/context_drop/sanitizer.js";
|
|
3
|
+
export { ContextDropApiClient } from "../app/javascript/context_drop/api-client.js";
|
|
4
|
+
export { ElementPicker } from "../app/javascript/context_drop/element-picker.js";
|
|
5
|
+
export { ContextDropWidgetElement, DEFAULT_SIDEBAR_WIDTH, registerContextDropWidget } from "../app/javascript/context_drop/widget.js";
|
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
import { type RemoteMcpEnvironmentConfig, type StorageAdapter } from "../server/types.js";
|
|
2
|
+
import { type EnvVarMap } from "./environment-router.js";
|
|
3
|
+
import { type RemoteMcpTransport } from "./remote-client.js";
|
|
4
|
+
export declare const MCP_PROTOCOL_VERSION = "2025-03-26";
|
|
5
|
+
export interface ContextDropMcpOptions {
|
|
6
|
+
storage: StorageAdapter;
|
|
7
|
+
hostEnvironment?: string;
|
|
8
|
+
environments?: Record<string, RemoteMcpEnvironmentConfig>;
|
|
9
|
+
localOnly?: boolean;
|
|
10
|
+
envVars?: EnvVarMap;
|
|
11
|
+
remoteTransport?: RemoteMcpTransport;
|
|
12
|
+
}
|
|
13
|
+
export interface JsonRpcResponse {
|
|
14
|
+
jsonrpc: "2.0";
|
|
15
|
+
id: string | number | null;
|
|
16
|
+
result?: unknown;
|
|
17
|
+
error?: {
|
|
18
|
+
code: number;
|
|
19
|
+
message: string;
|
|
20
|
+
};
|
|
21
|
+
}
|
|
22
|
+
export declare class ContextDropMcpDispatcher {
|
|
23
|
+
private readonly storage;
|
|
24
|
+
private readonly router;
|
|
25
|
+
private readonly remoteClient;
|
|
26
|
+
constructor(options: ContextDropMcpOptions);
|
|
27
|
+
dispatch(rawMessage: unknown): Promise<JsonRpcResponse | JsonRpcResponse[] | null>;
|
|
28
|
+
private dispatchSingle;
|
|
29
|
+
private handleToolCall;
|
|
30
|
+
private toolListEnvironments;
|
|
31
|
+
private toolListFeedbacks;
|
|
32
|
+
private fetchRemoteList;
|
|
33
|
+
private toolGetFeedback;
|
|
34
|
+
private fetchRemoteFeedback;
|
|
35
|
+
private toolGetFeedbackAttachment;
|
|
36
|
+
private toolUpdateFeedbackStatus;
|
|
37
|
+
private handleResourceRead;
|
|
38
|
+
private handlePromptGet;
|
|
39
|
+
private extractToolErrorMessage;
|
|
40
|
+
private jsonrpcResult;
|
|
41
|
+
private jsonrpcError;
|
|
42
|
+
}
|