Repository navigation
Expand file tree
/
Copy pathProgram.cs
More file actions
277 lines (248 loc) · 13.6 KB
/
Copy pathProgram.cs
File metadata and controls
277 lines (248 loc) · 13.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
using CodeGraph.Core;
using CodeGraph.Core.Live;
using CodeGraph.Mcp;
using CodeGraph.Web;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Http;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Logging;
using ModelContextProtocol.Protocol;
// --version answers without touching a graph: install scripts use it to check the executable starts at all.
if (args is ["--version"])
{
Console.WriteLine(ServerVersion());
return 0;
}
// Parse positional db path and optional --web PORT flag.
var dbPath = (string?)null;
var rootArg = (string?)null;
int? webPort = null;
var hostArgs = new List<string>();
for (var i = 0; i < args.Length; i++)
{
if ((args[i] is "--web" or "--port") && i + 1 < args.Length && int.TryParse(args[i + 1], out var p))
{
webPort = p;
i++; // skip the number
}
else if (args[i] is "--root" && i + 1 < args.Length)
{
rootArg = args[++i]; // the project folder, from a client that does not start servers in it
}
else if (!args[i].StartsWith("--") && dbPath is null)
{
dbPath ??= args[i]; // first non-flag arg is the db path
}
else
{
hostArgs.Add(args[i]);
}
}
dbPath ??= Environment.GetEnvironmentVariable("CODEGRAPH_DB");
// No explicit path: use this worktree's graph, found from the project folder. Claude Code, Codex, the Copilot CLI
// and the Cursor CLI start MCP servers in the project; an IDE passes it as --root ${workspaceFolder} (or
// CODEGRAPH_ROOT). One config then serves every worktree of a repo.
var discovered = false;
if (string.IsNullOrWhiteSpace(dbPath))
{
var start = Worktree.UsableRoot(rootArg) ?? Worktree.UsableRoot(Environment.GetEnvironmentVariable("CODEGRAPH_ROOT"))
?? Directory.GetCurrentDirectory();
dbPath = Worktree.FindDb(start) ?? Worktree.DefaultDbPath(start);
discovered = true;
}
dbPath = Path.GetFullPath(dbPath);
if (!discovered && !File.Exists(dbPath))
{
Console.Error.WriteLine("usage: codegraph-mcp [path-to-graph.db] [--root DIR] [--web PORT]");
Console.Error.WriteLine(" no path: finds .codegraph/graph.db from the current directory up to the git worktree root");
Console.Error.WriteLine(" --root DIR look from DIR instead (for clients that start servers elsewhere); or set CODEGRAPH_ROOT");
Console.Error.WriteLine(" --web PORT also serve the graph visualiser on http://localhost:PORT");
Console.Error.WriteLine(" set CODEGRAPH_DB as an alternative to the positional argument");
return 1;
}
Console.Error.WriteLine(File.Exists(dbPath)
? $"codegraph: serving {dbPath}"
: $"codegraph: no graph yet at {dbPath}; tools will say so until codegraph-index builds it");
var freshness = new IndexFreshness(dbPath, TimeSpan.FromSeconds(3));
// Telemetry stays beside the graph, one JSON line per tool call; CODEGRAPH_TELEMETRY=off keeps it in memory only.
var telemetrySetting = Environment.GetEnvironmentVariable("CODEGRAPH_TELEMETRY");
var telemetryOff = telemetrySetting is not null && telemetrySetting.Trim().ToLowerInvariant() is "0" or "off" or "false" or "no";
var telemetry = new Telemetry(telemetryOff ? null : Path.Combine(Path.GetDirectoryName(dbPath)!, "telemetry.jsonl"));
// The live daemon (codegraph-index --watch) answers on a pipe named after the graph; when it is up, a stale
// graph is flushed before answering, so STALE rows only appear for saves younger than the daemon's debounce.
var live = new LiveClient(dbPath);
var flushWait = TimeSpan.FromSeconds(8);
// Keeps the DuckDB instance loaded between calls (an open of 0.025 ms instead of 9.9 ms, BENCHMARKS round 2), letting go when idle or
// when the daemon is about to write.
var anchor = new GraphAnchor(dbPath);
// The graph in memory, as an IDE holds its symbols: walks follow arrays instead of running recursive SQL, and a
// call that needs nothing else never opens the database. Loaded in the background at start and again after every
// re-index; until it lands, calls read through DuckDB as before. CODEGRAPH_SNAPSHOT=off turns it off.
var snapshots = new GraphSnapshotCache(dbPath, anchor, poll: GraphSnapshotCache.Enabled ? TimeSpan.FromSeconds(1) : null);
// CODEGRAPH_DAEMON=auto: the first tool call starts a daemon when none runs; once per server process.
var autoStart = LiveLauncher.AutoStart ? 1 : 0;
void AddGraph(IServiceCollection services)
{
// Read-only and per request: DuckDB lets one process write or many read, so a store held open for the
// server's lifetime would lock out codegraph-index. Each tool call opens, queries and closes.
services.AddSingleton(freshness);
services.AddSingleton(telemetry);
services.AddSingleton(live);
services.AddSingleton(new SemanticSearch(EmbeddingSettings.FromEnvironment()));
services.AddSingleton(anchor);
services.AddSingleton(snapshots);
// The anchor lease is taken only if the call opens the database, and released with the store.
services.AddScoped(_ => GraphStore.OpenReader(
dbPath, GraphSnapshotCache.Enabled ? snapshots.Current : null, TimeSpan.FromSeconds(15), anchor.Acquire));
services
.AddMcpServer(options =>
{
// What a client shows on its page for this server (VS Code's "MCP Server: codegraph", Claude's /mcp).
options.ServerInfo = new Implementation
{
Name = "codegraph",
Title = "CodeGraph",
Version = ServerVersion(),
Description = "Compiler-built code graph for C#, TypeScript, Angular and other codebases: who calls this, what breaks if it changes, "
+ "which tests guard it, where is the code that does X. Read-only on the graph and local; only CODEGRAPH_EMBEDDINGS_URL sends search text to an endpoint.",
WebsiteUrl = "https://github.com/nerdcules/CodeGraph",
Icons = [new Icon { Source = LogoDataUri(), MimeType = "image/svg+xml", Sizes = ["any"] }],
};
options.ServerInstructions =
"""
codegraph answers structural questions about the indexed codebase from a compiler-built graph, so prefer
it to grepping or opening files. Typical turn: find_symbol (or search_code when you only know what the
code does) → impact_of / callers_of / references_to → snippet on the two or three declarations that matter.
Ids in [brackets] are opaque (Roslyn doc-comment ids for C#, ts:/scip: prefixes otherwise); pass them back verbatim, without the brackets. An answer that starts with
"⚠ STALE INDEX" is from a graph older than the working tree; rows ending in STALE come from edited files.
Call impact_of before changing anything shared, and tests_for / covered_by to find the tests that guard it.
When codegraph-index --watch is running (live_status), four tools answer from the live compiler (Roslyn, the TypeScript language service, or a language server):
symbol_at turns a file position into an id, live_references finds every use of a symbol including
framework and NuGet members, diagnostics reports compiler errors, and check_change compiles a proposed
file in memory and reports what it would break before you write it.
""";
})
.WithStdioServerTransport()
.WithToolsFromAssembly()
.WithRequestFilters(filters => filters.AddCallToolFilter(next => async (context, cancellationToken) =>
{
if (!File.Exists(dbPath))
{
return new CallToolResult
{
IsError = true,
Content = [new TextContentBlock { Text = $"no code graph at {dbPath}. Build it from this worktree with: codegraph-index <solution.sln>" }],
};
}
var scope = telemetry.Begin(context.Params?.Name ?? "?");
anchor.ReleaseIfSwapped();
// The daemon hears every save the moment it lands, before the (cached) freshness check would notice
// it, so ask it first; its idle answer costs a fifth of a millisecond.
var daemonStatus = await live.PingAsync(cancellationToken);
if (daemonStatus is null && Interlocked.Exchange(ref autoStart, 0) == 1)
{
Console.Error.WriteLine("codegraph: " + LiveLauncher.Start(dbPath, freshness.Check().Meta.GetValueOrDefault(CodeGraph.Core.MetaKeys.Solution)));
}
if (daemonStatus is { Loading: false } daemon
&& (daemon.Pending > 0 || daemon.Indexing || freshness.Check().IsStale))
{
// The daemon's write needs the file to itself; holding the anchor through the flush would deadlock it.
anchor.Release();
if (await live.FlushAsync(flushWait, cancellationToken) is not null)
{
freshness.Invalidate();
}
}
// Answers print ids in [brackets]; a model that copies the brackets along still gets its node.
if (context.Params?.Arguments is { } arguments)
{
NodeIds.NameTheId(context.Params.Name, arguments);
foreach (var key in arguments.Keys.Where(NodeIds.Arguments.Contains).ToList())
{
if (arguments[key].ValueKind == System.Text.Json.JsonValueKind.String
&& arguments[key].GetString() is { } raw && NodeIds.Unwrap(raw) is var id && id != raw)
{
arguments[key] = System.Text.Json.JsonSerializer.SerializeToElement(id);
}
}
}
CallToolResult result;
try
{
result = await next(context, cancellationToken);
}
catch (Exception e) when (ToolErrors.Explain(context.Params?.Name ?? "?", e, context.Params?.Arguments, dbPath) is { } explained)
{
// The SDK would answer "An error occurred invoking 'x'." and nothing else; say what failed and what to do.
result = new CallToolResult { IsError = true, Content = [new TextContentBlock { Text = explained }] };
}
catch
{
scope.Complete(0, isError: true);
throw;
}
if (freshness.Check().Warning() is { } warning)
{
result.Content.Insert(0, new TextContentBlock { Text = warning });
}
scope.Complete(result.Content.OfType<TextContentBlock>().Sum(block => (long)block.Text.Length), result.IsError == true);
return result;
}));
}
if (webPort.HasValue)
{
// ── Web + MCP mode ─────────────────────────────────────────
// Single process: stdio carries the MCP protocol, HTTP serves the browser UI.
var webBuilder = WebApplication.CreateBuilder(hostArgs.ToArray());
webBuilder.Configuration["urls"] = $"http://localhost:{webPort}";
webBuilder.Logging.AddConsole(opts => opts.LogToStandardErrorThreshold = LogLevel.Trace);
webBuilder.Logging.AddFilter("Microsoft.Hosting.Lifetime", LogLevel.Warning);
webBuilder.Logging.AddFilter("Microsoft.AspNetCore", LogLevel.Warning);
AddGraph(webBuilder.Services);
var webApp = webBuilder.Build();
MapWebApi(webApp);
Console.Error.WriteLine($"Graph visualiser → http://localhost:{webPort}");
await webApp.RunAsync();
}
else
{
// ── Pure MCP mode ──────────────────────────────────────────
var builder = Host.CreateApplicationBuilder(hostArgs.ToArray());
builder.Logging.AddConsole(opts => opts.LogToStandardErrorThreshold = LogLevel.Trace);
AddGraph(builder.Services);
await builder.Build().RunAsync();
}
return 0;
// ── Web API routes ────────────────────────────────────────────
static void MapWebApi(WebApplication app)
{
var html = ReadEmbeddedHtml();
app.MapGet("/", () => Results.Content(html, "text/html"));
app.MapGraphApi(); // shared with codegraph-web; see CodeGraph.Web/WebApi.cs
}
static string ServerVersion()
{
var assembly = System.Reflection.Assembly.GetExecutingAssembly();
var informational = assembly.GetCustomAttributes(typeof(System.Reflection.AssemblyInformationalVersionAttribute), false)
.OfType<System.Reflection.AssemblyInformationalVersionAttribute>()
.FirstOrDefault()?.InformationalVersion;
// Strip the source-link hash that the SDK appends ("1.2.0+abc123").
var version = informational?.Split('+')[0];
return string.IsNullOrEmpty(version) ? assembly.GetName().Version?.ToString(3) ?? "0.0.0" : version;
}
static string LogoDataUri()
{
using var stream = System.Reflection.Assembly.GetExecutingAssembly().GetManifestResourceStream("CodeGraph.Mcp.logo.svg")
?? throw new InvalidOperationException("Embedded logo not found. Rebuild the project.");
var svg = new StreamReader(stream).ReadToEnd();
return "data:image/svg+xml;base64," + Convert.ToBase64String(System.Text.Encoding.UTF8.GetBytes(svg));
}
static string ReadEmbeddedHtml()
{
var asm = System.Reflection.Assembly.GetExecutingAssembly();
using var stream = asm.GetManifestResourceStream("CodeGraph.Mcp.wwwroot.index.html")
?? asm.GetManifestResourceStream("CodeGraph.Mcp.index.html")
?? throw new InvalidOperationException("Embedded web UI not found. Rebuild the project.");
return new StreamReader(stream).ReadToEnd();
}