@philau2512/fast-context-mcp 1.5.3
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/LICENSE +21 -0
- package/README.md +306 -0
- package/package.json +53 -0
- package/scripts/link-local-bin.mjs +50 -0
- package/src/core.mjs +2518 -0
- package/src/directory-scorer.mjs +1086 -0
- package/src/executor.mjs +659 -0
- package/src/extract-key.mjs +334 -0
- package/src/project-path.mjs +47 -0
- package/src/protobuf.mjs +235 -0
- package/src/server.mjs +325 -0
- package/src/tree.mjs +82 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2025
|
|
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,306 @@
|
|
|
1
|
+
# Fast Context MCP
|
|
2
|
+
|
|
3
|
+
AI-driven semantic code search as an MCP tool — powered by Windsurf's reverse-engineered SWE-grep protocol.
|
|
4
|
+
|
|
5
|
+
Maintained fork: [philau2512/fast-context-mcp](https://github.com/philau2512/fast-context-mcp)
|
|
6
|
+
Upstream lineage: SammySnake / awei84 contributions (bootstrap hotspot, Devin auth, etc.)
|
|
7
|
+
|
|
8
|
+
Any MCP-compatible client (Claude Code, Claude Desktop, Cursor, etc.) can use this to search codebases with natural language queries. All tools are bundled via npm — **no system-level dependencies** needed (ripgrep via `@vscode/ripgrep`, tree via Node.js `fs`). Works on macOS, Windows, and Linux.
|
|
9
|
+
|
|
10
|
+
## How It Works
|
|
11
|
+
|
|
12
|
+
```
|
|
13
|
+
You: "where is the authentication logic?"
|
|
14
|
+
│
|
|
15
|
+
▼
|
|
16
|
+
┌─────────────────────────┐
|
|
17
|
+
│ Fast Context MCP │
|
|
18
|
+
│ (local MCP server) │
|
|
19
|
+
│ │
|
|
20
|
+
│ 1. Maps project → /codebase
|
|
21
|
+
│ 2. Sends query to Windsurf Devstral API
|
|
22
|
+
│ 3. AI generates rg/readfile/tree commands
|
|
23
|
+
│ 4. Executes commands locally (built-in rg)
|
|
24
|
+
│ 5. Returns results to AI
|
|
25
|
+
│ 6. Repeats for N rounds
|
|
26
|
+
│ 7. Returns file paths + line ranges
|
|
27
|
+
│ + suggested search keywords
|
|
28
|
+
└─────────────────────────┘
|
|
29
|
+
│
|
|
30
|
+
▼
|
|
31
|
+
Found 3 relevant files.
|
|
32
|
+
[1/3] /project/src/auth/handler.py (L10-60)
|
|
33
|
+
[2/3] /project/src/middleware/jwt.py (L1-40)
|
|
34
|
+
[3/3] /project/src/models/user.py (L20-80)
|
|
35
|
+
|
|
36
|
+
Suggested search keywords:
|
|
37
|
+
authenticate, jwt.*verify, session.*token
|
|
38
|
+
```
|
|
39
|
+
|
|
40
|
+
## Prerequisites
|
|
41
|
+
|
|
42
|
+
- **Node.js** >= 18
|
|
43
|
+
- **Windsurf / Devin account** — free tier works (needed for API key)
|
|
44
|
+
|
|
45
|
+
No need to install ripgrep — it's bundled via `@vscode/ripgrep`.
|
|
46
|
+
|
|
47
|
+
## Installation
|
|
48
|
+
|
|
49
|
+
### Option A: from GitHub (recommended for this fork)
|
|
50
|
+
|
|
51
|
+
```bash
|
|
52
|
+
# run latest from this repo (no global install)
|
|
53
|
+
npx -y github:philau2512/fast-context-mcp
|
|
54
|
+
```
|
|
55
|
+
|
|
56
|
+
### Option B: clone from source
|
|
57
|
+
|
|
58
|
+
```bash
|
|
59
|
+
git clone https://github.com/philau2512/fast-context-mcp.git
|
|
60
|
+
cd fast-context-mcp
|
|
61
|
+
npm install
|
|
62
|
+
```
|
|
63
|
+
|
|
64
|
+
### Option C: npm (after you publish)
|
|
65
|
+
|
|
66
|
+
```bash
|
|
67
|
+
npx -y fast-context-mcp
|
|
68
|
+
# or: npm install -g fast-context-mcp
|
|
69
|
+
```
|
|
70
|
+
|
|
71
|
+
## Setup
|
|
72
|
+
|
|
73
|
+
### 1. Get Your Windsurf / Devin API Key
|
|
74
|
+
|
|
75
|
+
The server auto-extracts the API key from your local Windsurf / Devin installation. You can also use the `extract_windsurf_key` MCP tool after setup, or set `WINDSURF_API_KEY` manually.
|
|
76
|
+
|
|
77
|
+
Lookup order:
|
|
78
|
+
1. **Linux/WSL**: Devin CLI credentials at `~/.local/share/devin/credentials.toml` (run `devin login` if missing)
|
|
79
|
+
2. Local SQLite DB candidates (**Windsurf** / **Devin** only)
|
|
80
|
+
|
|
81
|
+
| Platform | SQLite path candidates |
|
|
82
|
+
|----------|------------------------|
|
|
83
|
+
| macOS | `~/Library/Application Support/{Windsurf,Devin}/User/globalStorage/state.vscdb` |
|
|
84
|
+
| Windows | `%APPDATA%/{Windsurf,Devin}/User/globalStorage/state.vscdb` |
|
|
85
|
+
| Linux | `~/.config/{Windsurf,devin,Devin}/User/globalStorage/state.vscdb` |
|
|
86
|
+
|
|
87
|
+
On WSL/Linux, if a Windows-extracted key returns **403**, run `devin login` inside WSL so `~/.local/share/devin/credentials.toml` exists, then retry.
|
|
88
|
+
|
|
89
|
+
### 2. Configure MCP Client
|
|
90
|
+
|
|
91
|
+
#### Cursor
|
|
92
|
+
|
|
93
|
+
Add to Cursor MCP settings (`mcp.json`):
|
|
94
|
+
|
|
95
|
+
```json
|
|
96
|
+
{
|
|
97
|
+
"mcpServers": {
|
|
98
|
+
"fast-context": {
|
|
99
|
+
"command": "npx",
|
|
100
|
+
"args": ["-y", "github:philau2512/fast-context-mcp"],
|
|
101
|
+
"env": {
|
|
102
|
+
"FC_HIDE_EXTRACT_WINDSURF_KEY_TOOL": "1"
|
|
103
|
+
}
|
|
104
|
+
}
|
|
105
|
+
}
|
|
106
|
+
}
|
|
107
|
+
```
|
|
108
|
+
|
|
109
|
+
From a local clone (dev):
|
|
110
|
+
|
|
111
|
+
```json
|
|
112
|
+
{
|
|
113
|
+
"mcpServers": {
|
|
114
|
+
"fast-context": {
|
|
115
|
+
"command": "node",
|
|
116
|
+
"args": ["C:/path/to/fast-context-mcp/src/server.mjs"],
|
|
117
|
+
"env": {
|
|
118
|
+
"FC_HIDE_EXTRACT_WINDSURF_KEY_TOOL": "1"
|
|
119
|
+
}
|
|
120
|
+
}
|
|
121
|
+
}
|
|
122
|
+
}
|
|
123
|
+
```
|
|
124
|
+
|
|
125
|
+
#### Claude Code
|
|
126
|
+
|
|
127
|
+
Add to `~/.claude.json` under `mcpServers`:
|
|
128
|
+
|
|
129
|
+
```json
|
|
130
|
+
{
|
|
131
|
+
"fast-context": {
|
|
132
|
+
"command": "npx",
|
|
133
|
+
"args": ["-y", "github:philau2512/fast-context-mcp"],
|
|
134
|
+
"env": {
|
|
135
|
+
"WINDSURF_API_KEY": "sk-ws-01-xxxxx"
|
|
136
|
+
}
|
|
137
|
+
}
|
|
138
|
+
}
|
|
139
|
+
```
|
|
140
|
+
|
|
141
|
+
Or if installed from source:
|
|
142
|
+
|
|
143
|
+
```json
|
|
144
|
+
{
|
|
145
|
+
"fast-context": {
|
|
146
|
+
"command": "node",
|
|
147
|
+
"args": ["/absolute/path/to/fast-context-mcp/src/server.mjs"],
|
|
148
|
+
"env": {
|
|
149
|
+
"WINDSURF_API_KEY": "sk-ws-01-xxxxx"
|
|
150
|
+
}
|
|
151
|
+
}
|
|
152
|
+
}
|
|
153
|
+
```
|
|
154
|
+
|
|
155
|
+
#### Claude Desktop
|
|
156
|
+
|
|
157
|
+
Add to `claude_desktop_config.json` under `mcpServers`:
|
|
158
|
+
|
|
159
|
+
```json
|
|
160
|
+
{
|
|
161
|
+
"fast-context": {
|
|
162
|
+
"command": "npx",
|
|
163
|
+
"args": ["-y", "github:philau2512/fast-context-mcp"],
|
|
164
|
+
"env": {
|
|
165
|
+
"WINDSURF_API_KEY": "sk-ws-01-xxxxx"
|
|
166
|
+
}
|
|
167
|
+
}
|
|
168
|
+
}
|
|
169
|
+
```
|
|
170
|
+
|
|
171
|
+
> If `WINDSURF_API_KEY` is omitted, the server auto-discovers it from your local Windsurf / Devin installation.
|
|
172
|
+
|
|
173
|
+
## Environment Variables
|
|
174
|
+
|
|
175
|
+
| Variable | Default | Description |
|
|
176
|
+
|----------|---------|-------------|
|
|
177
|
+
| `WINDSURF_API_KEY` | *(auto-discover)* | Windsurf API key |
|
|
178
|
+
| `FC_MAX_TURNS` | `3` | Search rounds per query (more = deeper but slower) |
|
|
179
|
+
| `FC_MAX_COMMANDS` | `8` | Max parallel commands per round |
|
|
180
|
+
| `FC_TIMEOUT_MS` | `30000` | Connect-Timeout-Ms for streaming requests |
|
|
181
|
+
| `FC_HIDE_EXTRACT_WINDSURF_KEY_TOOL` | `false` | Hide `extract_windsurf_key` from MCP tools when set to `1`/`true`/`yes`/`on` |
|
|
182
|
+
| `FC_RESULT_MAX_LINES` | `50` | Max lines per command output (truncation) |
|
|
183
|
+
| `FC_LINE_MAX_CHARS` | `250` | Max characters per output line (truncation) |
|
|
184
|
+
| `FC_INCLUDE_SNIPPETS` | `false` | Default for returning code snippets with search results |
|
|
185
|
+
| `FC_REPO_MAP_MODE` | `bootstrap_hotspot` | Repo-map strategy (`bootstrap_hotspot` or `classic`) |
|
|
186
|
+
| `FC_BOOTSTRAP_ENABLED` | `true` | Enable the standalone bootstrap phase for hotspot discovery |
|
|
187
|
+
| `FC_BOOTSTRAP_TREE_DEPTH` | `1` | Directory depth used by the bootstrap repo map (1-3) |
|
|
188
|
+
| `FC_BOOTSTRAP_MAX_TURNS` | `2` | Search turns used by the bootstrap phase (1-3) |
|
|
189
|
+
| `FC_BOOTSTRAP_MAX_COMMANDS` | `6` | Max commands per bootstrap turn (1-8) |
|
|
190
|
+
| `FC_HOTSPOT_TOP_K` | `4` | Number of hotspot top-level directories to append (0-8) |
|
|
191
|
+
| `FC_HOTSPOT_TREE_DEPTH` | `2` | Directory depth for each hotspot subtree (1-4) |
|
|
192
|
+
| `FC_HOTSPOT_MAX_BYTES` | `122880` | Byte budget for optimized repo-map output |
|
|
193
|
+
| `WS_MODEL` | `MODEL_SWE_1_6_FAST` | Windsurf model name |
|
|
194
|
+
| `WS_APP_VER` | `1.48.2` | Windsurf app version (protocol metadata) |
|
|
195
|
+
| `WS_LS_VER` | `1.9544.35` | Windsurf language server version (protocol metadata) |
|
|
196
|
+
|
|
197
|
+
## Available Models
|
|
198
|
+
|
|
199
|
+
The model can be changed by setting `WS_MODEL` (see environment variables above).
|
|
200
|
+
|
|
201
|
+

|
|
202
|
+
|
|
203
|
+
Default: `MODEL_SWE_1_6_FAST` — fastest speed, richest grep keywords, finest location granularity.
|
|
204
|
+
|
|
205
|
+
## MCP Tools
|
|
206
|
+
|
|
207
|
+
### `fast_context_search`
|
|
208
|
+
|
|
209
|
+
AI-driven semantic code search with tunable parameters. For best semantic search quality, write queries primarily in English; add local-language business terms only when useful.
|
|
210
|
+
|
|
211
|
+
| Parameter | Type | Required | Default | Description |
|
|
212
|
+
|-----------|------|----------|---------|-------------|
|
|
213
|
+
| `query` | string | Yes | — | Natural language search query. English is recommended for best semantic matching; add local-language business terms when useful. |
|
|
214
|
+
| `project_path` | string | **Yes** | — | Absolute path to project root directory |
|
|
215
|
+
| `tree_depth` | integer | No | `3` | Directory tree depth for repo map (0-6, 0 = auto). Higher = more context but larger payload. Auto falls back to lower depth if tree exceeds 250KB. Use 1-2 for huge monorepos (>5000 files), 3 for most projects, 4-6 for small projects. |
|
|
216
|
+
| `max_turns` | integer | No | `3` | Search rounds (1-5). More = deeper search but slower. Use 1-2 for simple lookups, 3 for most queries, 4-5 for complex analysis. |
|
|
217
|
+
| `max_results` | integer | No | `10` | Maximum number of files to return (1-30). Smaller = more focused, larger = broader exploration. |
|
|
218
|
+
| `exclude_paths` | string[] | No | `[]` | Directory/file patterns to exclude from repo map and search context. Useful for large repos or noisy generated files. |
|
|
219
|
+
| `include_code_snippets` | boolean | No | `false` | Include code snippets in the response. Defaults to file paths, line ranges, and grep keywords only. |
|
|
220
|
+
|
|
221
|
+
Returns:
|
|
222
|
+
1. **Relevant files** with line ranges
|
|
223
|
+
2. **Suggested search keywords** (rg patterns used during AI search)
|
|
224
|
+
3. **Diagnostic metadata** (`[config]` line showing project_path, actual tree_depth used, tree size, and whether fallback occurred)
|
|
225
|
+
|
|
226
|
+
Example output:
|
|
227
|
+
```
|
|
228
|
+
Found 3 relevant files.
|
|
229
|
+
|
|
230
|
+
[1/3] /project/src/auth/handler.py (L10-60, L120-180)
|
|
231
|
+
[2/3] /project/src/middleware/jwt.py (L1-40)
|
|
232
|
+
[3/3] /project/src/models/user.py (L20-80)
|
|
233
|
+
|
|
234
|
+
grep keywords: authenticate, jwt.*verify, session.*token
|
|
235
|
+
|
|
236
|
+
[config] project_path=/project, tree_depth=3, tree_size=12.5KB, max_turns=3
|
|
237
|
+
```
|
|
238
|
+
|
|
239
|
+
Error output includes status-specific hints:
|
|
240
|
+
```
|
|
241
|
+
Error: Request failed: HTTP 403
|
|
242
|
+
|
|
243
|
+
[hint] 403 Forbidden: Authentication failed. The API key may be expired or revoked.
|
|
244
|
+
Try re-extracting with extract_windsurf_key, or set a fresh WINDSURF_API_KEY env var.
|
|
245
|
+
```
|
|
246
|
+
|
|
247
|
+
```
|
|
248
|
+
Error: Request failed: HTTP 413
|
|
249
|
+
|
|
250
|
+
[diagnostic] tree_depth_used=3, tree_size=280.0KB (auto fell back from requested depth)
|
|
251
|
+
[hint] If the error is payload-related, try a lower tree_depth value.
|
|
252
|
+
```
|
|
253
|
+
|
|
254
|
+
### `extract_windsurf_key`
|
|
255
|
+
|
|
256
|
+
Extract Windsurf / Devin API Key from local installation (CLI credentials.toml on Linux/WSL, then SQLite DBs). No parameters.
|
|
257
|
+
|
|
258
|
+
Set `FC_HIDE_EXTRACT_WINDSURF_KEY_TOOL=1` at MCP server startup to hide this tool from `tools/list`. Internal auto-discovery for `fast_context_search` is unaffected.
|
|
259
|
+
|
|
260
|
+
## Project Structure
|
|
261
|
+
|
|
262
|
+
```
|
|
263
|
+
fast-context-mcp/
|
|
264
|
+
├── package.json
|
|
265
|
+
├── src/
|
|
266
|
+
│ ├── server.mjs # MCP server entry point
|
|
267
|
+
│ ├── core.mjs # Auth, message building, streaming, search loop
|
|
268
|
+
│ ├── executor.mjs # Tool executor: rg, readfile, tree, ls, glob
|
|
269
|
+
│ ├── extract-key.mjs # Windsurf API Key extraction (SQLite)
|
|
270
|
+
│ └── protobuf.mjs # Protobuf encoder/decoder + Connect-RPC frames
|
|
271
|
+
├── README.md
|
|
272
|
+
└── LICENSE
|
|
273
|
+
```
|
|
274
|
+
|
|
275
|
+
## How the Search Works
|
|
276
|
+
|
|
277
|
+
1. Project directory is mapped to virtual `/codebase` path
|
|
278
|
+
2. Directory tree generated at requested depth (default L=3), with **automatic fallback** to lower depth if tree exceeds 250KB
|
|
279
|
+
3. Query + directory tree sent to Windsurf's Devstral model via Connect-RPC/Protobuf
|
|
280
|
+
4. Devstral generates tool commands (ripgrep, file reads, tree, ls, glob)
|
|
281
|
+
5. Commands executed locally in parallel (up to `FC_MAX_COMMANDS` per round)
|
|
282
|
+
6. Results sent back to Devstral for the next round
|
|
283
|
+
7. After `max_turns` rounds, Devstral returns file paths + line ranges
|
|
284
|
+
8. All rg patterns used during search are collected as suggested keywords
|
|
285
|
+
9. Diagnostic metadata appended to help the calling AI tune parameters
|
|
286
|
+
|
|
287
|
+
## Technical Details
|
|
288
|
+
|
|
289
|
+
- **Protocol**: Connect-RPC over HTTP/1.1, Protobuf encoding, gzip compression
|
|
290
|
+
- **Model**: Devstral (`MODEL_SWE_1_6_FAST`, configurable)
|
|
291
|
+
- **Local tools**: `rg` (bundled via @vscode/ripgrep), `readfile` (Node.js fs), `tree` (Node.js fs), `ls` (Node.js fs), `glob` (Node.js fs)
|
|
292
|
+
- **Auth**: API Key → JWT (auto-fetched per session)
|
|
293
|
+
- **Runtime**: Node.js >= 18 (ESM)
|
|
294
|
+
|
|
295
|
+
### Dependencies
|
|
296
|
+
|
|
297
|
+
| Package | Purpose |
|
|
298
|
+
|---------|---------|
|
|
299
|
+
| `@modelcontextprotocol/sdk` | MCP server framework |
|
|
300
|
+
| `@vscode/ripgrep` | Bundled ripgrep binary (cross-platform) |
|
|
301
|
+
| `sql.js` | Read Windsurf's local SQLite DB |
|
|
302
|
+
| `zod` | Schema validation (MCP SDK requirement) |
|
|
303
|
+
|
|
304
|
+
## License
|
|
305
|
+
|
|
306
|
+
MIT
|
package/package.json
ADDED
|
@@ -0,0 +1,53 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@philau2512/fast-context-mcp",
|
|
3
|
+
"version": "1.5.3",
|
|
4
|
+
"description": "AI-driven semantic code search MCP server (Node.js) — Windsurf/Devin protocol",
|
|
5
|
+
"type": "module",
|
|
6
|
+
"main": "src/server.mjs",
|
|
7
|
+
"bin": {
|
|
8
|
+
"fast-context-mcp": "src/server.mjs"
|
|
9
|
+
},
|
|
10
|
+
"files": [
|
|
11
|
+
"src/",
|
|
12
|
+
"scripts/",
|
|
13
|
+
"README.md",
|
|
14
|
+
"LICENSE"
|
|
15
|
+
],
|
|
16
|
+
"scripts": {
|
|
17
|
+
"start": "node src/server.mjs",
|
|
18
|
+
"postinstall": "node scripts/link-local-bin.mjs",
|
|
19
|
+
"test": "node --test"
|
|
20
|
+
},
|
|
21
|
+
"engines": {
|
|
22
|
+
"node": ">=18"
|
|
23
|
+
},
|
|
24
|
+
"repository": {
|
|
25
|
+
"type": "git",
|
|
26
|
+
"url": "git+https://github.com/philau2512/fast-context-mcp.git"
|
|
27
|
+
},
|
|
28
|
+
"homepage": "https://github.com/philau2512/fast-context-mcp#readme",
|
|
29
|
+
"bugs": {
|
|
30
|
+
"url": "https://github.com/philau2512/fast-context-mcp/issues"
|
|
31
|
+
},
|
|
32
|
+
"keywords": [
|
|
33
|
+
"mcp",
|
|
34
|
+
"code-search",
|
|
35
|
+
"semantic-search",
|
|
36
|
+
"windsurf",
|
|
37
|
+
"devin",
|
|
38
|
+
"devstral",
|
|
39
|
+
"ripgrep",
|
|
40
|
+
"claude",
|
|
41
|
+
"cursor",
|
|
42
|
+
"ai"
|
|
43
|
+
],
|
|
44
|
+
"author": "philau2512",
|
|
45
|
+
"dependencies": {
|
|
46
|
+
"@modelcontextprotocol/sdk": "^1.0.0",
|
|
47
|
+
"@vscode/ripgrep": "^1.15.9",
|
|
48
|
+
"scule": "^1.3.0",
|
|
49
|
+
"sql.js": "^1.14.0",
|
|
50
|
+
"zod": "^3.23.0"
|
|
51
|
+
},
|
|
52
|
+
"license": "MIT"
|
|
53
|
+
}
|
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
/**
|
|
3
|
+
* 为源码仓库创建本地 bin 自链接。
|
|
4
|
+
*
|
|
5
|
+
* npm/npx 在当前目录的 package 名称与请求包名相同时,会优先使用当前包。
|
|
6
|
+
* 这种情况下不会像安装依赖那样自动生成 node_modules/.bin/fast-context-mcp,
|
|
7
|
+
* 导致在本仓库根目录执行 `npx fast-context-mcp@x` 时出现 command not found。
|
|
8
|
+
*/
|
|
9
|
+
|
|
10
|
+
import { chmodSync, existsSync, mkdirSync, readFileSync, rmSync, symlinkSync, writeFileSync } from "node:fs";
|
|
11
|
+
import { dirname, join, relative, resolve } from "node:path";
|
|
12
|
+
import { fileURLToPath } from "node:url";
|
|
13
|
+
|
|
14
|
+
const repoRoot = resolve(dirname(fileURLToPath(import.meta.url)), "..");
|
|
15
|
+
const initCwd = process.env.INIT_CWD ? resolve(process.env.INIT_CWD) : repoRoot;
|
|
16
|
+
if (initCwd !== repoRoot) {
|
|
17
|
+
process.exit(0);
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
const packageJsonPath = join(repoRoot, "package.json");
|
|
21
|
+
const pkg = JSON.parse(readFileSync(packageJsonPath, "utf8"));
|
|
22
|
+
|
|
23
|
+
const binTarget = pkg.bin?.["fast-context-mcp"];
|
|
24
|
+
if (!binTarget) {
|
|
25
|
+
process.exit(0);
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
const binDir = join(repoRoot, "node_modules", ".bin");
|
|
29
|
+
const entryPath = join(repoRoot, binTarget);
|
|
30
|
+
const linkPath = join(binDir, "fast-context-mcp");
|
|
31
|
+
|
|
32
|
+
mkdirSync(binDir, { recursive: true });
|
|
33
|
+
chmodSync(entryPath, 0o755);
|
|
34
|
+
|
|
35
|
+
if (process.platform === "win32") {
|
|
36
|
+
writeFileSync(
|
|
37
|
+
`${linkPath}.cmd`,
|
|
38
|
+
`@ECHO off\r\nnode "%~dp0\\..\\..\\${binTarget.replaceAll("/", "\\")}" %*\r\n`,
|
|
39
|
+
"utf8",
|
|
40
|
+
);
|
|
41
|
+
} else {
|
|
42
|
+
const relativeTarget = relative(binDir, entryPath);
|
|
43
|
+
try {
|
|
44
|
+
rmSync(linkPath, { force: true });
|
|
45
|
+
symlinkSync(relativeTarget, linkPath);
|
|
46
|
+
} catch {
|
|
47
|
+
writeFileSync(linkPath, `#!/bin/sh\nexec node "${relativeTarget}" "$@"\n`, "utf8");
|
|
48
|
+
}
|
|
49
|
+
chmodSync(linkPath, 0o755);
|
|
50
|
+
}
|