summaryrefslogtreecommitdiff
path: root/cmd/hexai-mcp-server/main.go
diff options
context:
space:
mode:
authorPaul Buetow <paul@buetow.org>2026-06-11 08:34:41 +0300
committerPaul Buetow <paul@buetow.org>2026-06-11 08:34:41 +0300
commite95f3fdf0a66ba05ba2c8fb7e755e107f9cf7991 (patch)
tree412c7e21ec9c317beb99ed7fe0d4d93a7dacbe50 /cmd/hexai-mcp-server/main.go
parent73dadb573f92dca310036e8793932e94277abd62 (diff)
Thread context.Context through blocking I/O entry points
Accept ctx as the first parameter on the blocking I/O entry points and propagate it to downstream blocking calls so the work is cancellable from the process entry point: - appconfig.Load / LoadWithOptions: honor ctx before the blocking file reads, returning defaults on a cancelled context. - LSP: lsp.Server.Run(ctx) ties the serve loop to the caller context via a new watchParentContext bridge (cancels the server context, aborting in-flight LLM work). Threaded through hexailsp.Run/RunWithConfig/ RunWithFactory and runtimeconfig.Store.Reload. - MCP: mcp.Server.Run(ctx) stops accepting requests once ctx is cancelled; threaded through hexaimcp.Run/RunWithFactory/RunBackfill. - editor: RunEditor/OpenTempAndEdit/OpenFile take ctx and use exec.CommandContext so a cancelled context kills the editor subprocess; threaded through hexaicli, hexaiaction and askcli call sites. Top-level callers (cmd/hexai-lsp-server, cmd/hexai-mcp-server) now build a signal-cancelled context (SIGINT/SIGTERM) so shutdown tears the run down cleanly. Updated comments to explain the cancellation flow and added cancellation tests for the LSP/MCP loops, editor, and config load. All tests pass with -race; cross-package coverage 86.2%. Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
Diffstat (limited to 'cmd/hexai-mcp-server/main.go')
-rw-r--r--cmd/hexai-mcp-server/main.go16
1 files changed, 12 insertions, 4 deletions
diff --git a/cmd/hexai-mcp-server/main.go b/cmd/hexai-mcp-server/main.go
index ac88178..03150bb 100644
--- a/cmd/hexai-mcp-server/main.go
+++ b/cmd/hexai-mcp-server/main.go
@@ -2,11 +2,14 @@
package main
import (
+ "context"
"flag"
"fmt"
"io"
"os"
+ "os/signal"
"path/filepath"
+ "syscall"
"codeberg.org/snonux/hexai/internal"
"codeberg.org/snonux/hexai/internal/appconfig"
@@ -100,7 +103,11 @@ func runMain(args []string, stdin io.Reader, stdout, stderr io.Writer) int {
syncAll: *syncAll,
showVersion: *showVersion,
}
- if err := run(opts, stdin, stdout, stderr); err != nil {
+ // Cancel the run on interrupt/terminate so the serve loop exits cleanly.
+ ctx, stop := signal.NotifyContext(context.Background(), os.Interrupt, syscall.SIGTERM)
+ defer stop()
+
+ if err := run(ctx, opts, stdin, stdout, stderr); err != nil {
fmt.Fprintf(stderr, "error: %v\n", err)
return 1
}
@@ -109,7 +116,8 @@ func runMain(args []string, stdin io.Reader, stdout, stderr io.Writer) int {
// run executes the MCP server logic with the given options and I/O streams.
// CLI flag values are passed via MCPOverrides instead of environment variables.
-func run(opts mcpOptions, stdin io.Reader, stdout, stderr io.Writer) error {
+// ctx is threaded into the server/backfill so they stop on shutdown signals.
+func run(ctx context.Context, opts mcpOptions, stdin io.Reader, stdout, stderr io.Writer) error {
if opts.showVersion {
fmt.Fprintln(stdout, internal.Version)
return nil
@@ -119,10 +127,10 @@ func run(opts mcpOptions, stdin io.Reader, stdout, stderr io.Writer) error {
// Handle backfill operation
if opts.syncAll {
- return runBackfill(opts.logPath, opts.configPath, overrides)
+ return runBackfill(ctx, opts.logPath, opts.configPath, overrides)
}
- return runMCP(opts.logPath, opts.configPath, overrides, stdin, stdout, stderr)
+ return runMCP(ctx, opts.logPath, opts.configPath, overrides, stdin, stdout, stderr)
}
// defaultLogPath returns the default MCP log file path in the state directory.