This document outlines the tasks required to enhance CodeCompass.
- Goal: Decouple
startServerfromprocess.exit.startServershould report startup success by resolving its promise, or failure by throwing a customServerStartupError(which includes anexitCodeproperty). This allows the caller (e.g.,src/index.ts) to decide the ultimate action. - Status: Completed.
startServernow throwsServerStartupError, andsrc/index.tshandles process exit.
- Goal: Provide flexible and prioritized ways for users to configure the HTTP port.
- Tasks:
ConfigServiceUpdate: (Completed)HTTP_PORTis now loaded from environment variables (HTTP_PORT) with a fallback to a default value. It is not loaded from or persisted tomodel-config.json.- CLI Port Argument: (Completed) Added a CLI argument (
--port <number>) insrc/index.tsfor specifying the HTTP port. This argument takes the highest precedence.
- Goal: Transition CodeCompass to use
stdioas the primary transport for MCP communication, enhancing privacy and suitability for on-premise local LLM usage. The HTTP server will be simplified to handle utility functions like repository sync triggers and status checks. - Status: Completed.
- Tasks:
src/lib/server.ts-stdioMCP Server Implementation: (Completed)- Modified
startServerto initialize theMcpServerinstance to use aStdioServerTransport. configureMcpServerInstancecorrectly sets up resources, tools, and prompts for thestdio-basedMcpServer.
- Modified
src/lib/server.ts- Utility HTTP Server Refinement: (Completed)- Refactored the Express.js app setup.
- HTTP server only exposes
/api/ping,/api/indexing-status,/api/repository/notify-update. - Removed the
/mcpHTTP endpoint.
src/index.ts- Adapt Server Startup: (Completed)startServerHandlercorrectly launches thestdio-first server and handles new error/resolution logic fromstartServer.- Logging indicates
stdioMCP and utility HTTP endpoints, including relay mode.
- Port Conflict Handling for Utility HTTP Server: (Completed as part of Phase 3 implementation)
- Logic implemented as part of Phase 3.
- Documentation: (Completed)
README.mdupdated forstdioMCP and utility HTTP endpoints, including port conflict behavior.
- Goal: If the CodeCompass CLI is invoked with a command intended for client-side execution (e.g.,
codecompass agent_query "..."), it should execute the command as an MCP client, primarily usingstdio. - Tasks for
src/index.ts:- Argument Parsing for Client Mode: (Completed) Implemented logic in
src/index.tsto parse CLI arguments and distinguish "client execution" commands (based onKNOWN_TOOLS) from "start server" commands. - Adapt
executeClientCommandforstdioMCP: (Completed)- Primary Communication via
stdio: (Completed)executeClientCommandnow spawns a dedicated CodeCompass server process and communicates with it overstdiousingStdioClientTransport.- The CLI always spawns its own dedicated server instance for the command.
- Fallback/Utility Server Check: (Removed) The HTTP server ping logic was removed from
executeClientCommandin favor of always spawning a server forstdiocommunication for client tool calls. - MCP client setup uses
StdioClientTransport. (Completed) client.callTool()works overstdio. (Completed)
- Primary Communication via
- Session ID Management for Client Calls: (Considered/Supported) The current mechanism allows users to pass
sessionIdwithin the JSON parameters for tools that support it. Help text updated to reflect this. No further client-side generation or automatic management of session IDs is planned for this phase. - Further Enhancements (Future):
- Add comprehensive unit/integration tests for the
stdio-based client mode functionality. (Completed - Integration tests for stdio client-server interactions cover this) - Refactor CLI to use
yargslibrary: (Completed)src/index.tsrefactored to useyargsfor argument parsing, command handling, and help generation. This addresses the evaluation of needing a dedicated CLI library. - Explore more advanced output formatting options if needed for specific tools.
- Add comprehensive unit/integration tests for the
- Argument Parsing for Client Mode: (Completed) Implemented logic in
- Goal: Define behavior when the utility HTTP server (responsible for sync, status) encounters a port conflict.
- Status: Option C selected and implemented.
- Tasks:
- Decision on Conflict Resolution: (Completed)
- Option C selected and implemented: If the configured utility HTTP port is in use by another CodeCompass utility server, the current instance will not start its own utility HTTP server. Instead, its
stdio-based MCP server will handle relevant MCP tool requests (e.g., for indexing status or triggering updates) by making HTTP client calls to the existing utility HTTP server on the originalHTTP_PORT. If the port is used by a non-CodeCompass service, this instance will log an error and exit.
- Option C selected and implemented: If the configured utility HTTP port is in use by another CodeCompass utility server, the current instance will not start its own utility HTTP server. Instead, its
- Implementation: (Completed)
- Modified
startServerinsrc/lib/server.ts:- On
EADDRINUSE, pings the port. - If a CodeCompass server responds: Utility HTTP server for the current instance is not started.
configService.IS_UTILITY_SERVER_DISABLEDandconfigService.RELAY_TARGET_UTILITY_PORTare set.startServerresolves successfully forstdioMCP. - If a non-CodeCompass service responds or ping fails: Throws a
ServerStartupErrorto cause the instance to exit.
- On
- Modified MCP tool
get_indexing_statusand addedtrigger_repository_updatetool insrc/lib/server.tsto relay requests viaaxiosifIS_UTILITY_SERVER_DISABLEDis true.
- Modified
- Testing: (Partially Addressed) Unit tests for utility HTTP port conflict handling and MCP tool relaying in
src/tests/server.test.tshave been updated and refined. Some timeouts and assertion issues persist, particularly forstartProxyServerandMCP Tool Relayingsuites. Unit tests forsrc/index.tsstill require significant attention. - Documentation: (Completed)
README.mdupdated regarding utility HTTP port conflicts and relay behavior.- CLI help text (via
KNOWN_TOOLSinsrc/index.ts) implicitly updated by addingtrigger_repository_update.
- Decision on Conflict Resolution: (Completed)
- Original Goal: If CodeCompass CLI attempts to start a server on a port already occupied by another CodeCompass instance, start a lightweight HTTP proxy on a different port to forward MCP requests.
- Status: Core logic was implemented. However, with the shift to
stdio-first MCP, this HTTP-to-HTTP proxy for MCP calls is no longer the primary strategy. - Completed Sub-Tasks (Historical):
ServerStartupErrorenhancement.findFreePortutility.startProxyServerfunction (HTTP-to-HTTP proxy).startServerHandlerlogic to callstartProxyServer. -- Reason for Deprioritization: The primary MCP interface will bestdio. The utility HTTP server port conflict will be handled differently (see Phase 3 above, Option C does not usefindFreePortfor the utility server). Tests forfindFreePortare primarily relevant to the deprioritized HTTP-to-HTTP proxy. Tests forstartProxyServer(HTTP-to-HTTP MCP proxy) are no longer a priority.
- Implement Phase 1 & 3: (Completed) Core architecture shifted to
stdio-first MCP, and utility HTTP server port conflict handling (Option C) implemented. Unit tests updated/added. - Adapt Phase 2: Enhance CLI for Client Mode for
stdio: (Implementation Completed)stdio-based client communication inexecuteClientCommandimplemented (spawns server process).
- Update Documentation: (Completed for Phase 1, 2 & 3)
README.md,TODO.md,CHANGELOG.md,RETROSPECTION.mdupdated. - Testing:
- (Completed) Unit tests for utility HTTP port conflict handling and MCP tool relaying in
src/tests/server.test.ts. - (Completed) Unit tests for
src/index.tsCLI behavior (including stdio client mode) updated. - (Completed) Develop comprehensive integration tests for
stdioserver and client interactions.- Initial test file structure and basic connection test created. (a52448e)
- Mocks for Qdrant, Ollama, and LLMProvider set up. (a52448e)
- Added integration tests for indexing, search_code, and agent_query. (212f0ff)
- Unmocked
indexRepositoryandgetGlobalIndexingStatusin integration tests for more realistic testing. (230f232) - Added integration tests for
get_changelogandtrigger_repository_update. (230f232) - Enhanced Qdrant client mock in integration tests to support
scrollanddeletemethods for stale entry cleanup. (230f232) - Added integration tests for
switch_suggestion_model,get_session_history,generate_suggestion, andget_repository_context. (dbbeee9) - Fixed premature server exits in integration tests by ensuring spawned servers use dynamic utility HTTP ports. (95286ae)
- Corrected
StdioClientTransportparameters in integration tests. (0f7f266)
- (Completed) Unit tests for utility HTTP port conflict handling and MCP tool relaying in