forked from Aider-AI/aider
-
Notifications
You must be signed in to change notification settings - Fork 40
Add an MCP Server Manager #361
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
34f3e23
Add an MCP Server Manager
gopar 425afb1
Remove unneeded magic methods
gopar b880cae
Merge branch 'main' into mcp-server-manager
gopar 3382299
Handle asyncio errors and exit flow
gopar ffe4d7d
Working version with mcp manager
gopar b1df0b7
Update tests
gopar 621b419
Simplify manager class
gopar da5e550
Update agent coder to use mcp server
gopar File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,154 +1,14 @@ | ||
| import json | ||
| from pathlib import Path | ||
|
|
||
| from cecli.mcp.server import HttpStreamingServer, McpServer, SseServer | ||
|
|
||
|
|
||
| def _parse_mcp_servers_from_json_string(json_string, io, verbose=False, mcp_transport="stdio"): | ||
| """Parse MCP servers from a JSON string.""" | ||
| servers = [] | ||
|
|
||
| try: | ||
| config = json.loads(json_string) | ||
| if verbose: | ||
| io.tool_output("Loading MCP servers from provided JSON") | ||
|
|
||
| if "mcpServers" in config: | ||
| for name, server_config in config["mcpServers"].items(): | ||
| if verbose: | ||
| io.tool_output(f"Loading MCP server: {name}") | ||
|
|
||
| # Create a server config with name included | ||
| server_config["name"] = name | ||
| transport = server_config.get("transport", mcp_transport) | ||
| if transport == "stdio": | ||
| servers.append(McpServer(server_config, io=io, verbose=verbose)) | ||
| elif transport == "http": | ||
| servers.append(HttpStreamingServer(server_config, io=io, verbose=verbose)) | ||
| elif transport == "sse": | ||
| servers.append(SseServer(server_config, io=io, verbose=verbose)) | ||
|
|
||
| if verbose: | ||
| io.tool_output(f"Loaded {len(servers)} MCP servers") | ||
| return servers | ||
| else: | ||
| io.tool_warning("No 'mcpServers' key found in MCP config") | ||
| except json.JSONDecodeError: | ||
| io.tool_error("Invalid JSON in MCP config") | ||
| except Exception as e: | ||
| io.tool_error(f"Error loading MCP config: {e}") | ||
|
|
||
| return servers | ||
|
|
||
|
|
||
| def _resolve_mcp_config_path(file_path, io, verbose=False): | ||
| """Resolve MCP config file path relative to closest cecli.conf.yml, git directory, or CWD.""" | ||
| if not file_path: | ||
| return None | ||
|
|
||
| # If the path is absolute or already exists, use it as-is | ||
| path = Path(file_path) | ||
| if path.is_absolute() or path.exists(): | ||
| return str(path.resolve()) | ||
|
|
||
| # Search for the closest cecli.conf.yml in parent directories | ||
| current_dir = Path.cwd() | ||
| conf_path = None | ||
|
|
||
| for parent in [current_dir] + list(current_dir.parents): | ||
| conf_file = parent / ".cecli.conf.yml" | ||
| if conf_file.exists(): | ||
| conf_path = parent | ||
| break | ||
|
|
||
| # If cecli.conf.yml found, try relative to that directory | ||
| if conf_path: | ||
| resolved_path = conf_path / file_path | ||
| if resolved_path.exists(): | ||
| if verbose: | ||
| io.tool_output(f"Resolved MCP config relative to cecli.conf.yml: {resolved_path}") | ||
| return str(resolved_path.resolve()) | ||
|
|
||
| # Try to find git root directory | ||
| git_root = None | ||
| try: | ||
| import git | ||
|
|
||
| repo = git.Repo(search_parent_directories=True) | ||
| git_root = Path(repo.working_tree_dir) | ||
| except (ImportError, git.InvalidGitRepositoryError, FileNotFoundError): | ||
| pass | ||
|
|
||
| # If git root found, try relative to that directory | ||
| if git_root: | ||
| resolved_path = git_root / file_path | ||
| if resolved_path.exists(): | ||
| if verbose: | ||
| io.tool_output(f"Resolved MCP config relative to git root: {resolved_path}") | ||
| return str(resolved_path.resolve()) | ||
|
|
||
| # Finally, try relative to current working directory | ||
| resolved_path = current_dir / file_path | ||
| if resolved_path.exists(): | ||
| if verbose: | ||
| io.tool_output(f"Resolved MCP config relative to CWD: {resolved_path}") | ||
| return str(resolved_path.resolve()) | ||
|
|
||
| # If none found, return the original path (will trigger FileNotFoundError) | ||
| return str(path.resolve()) | ||
|
|
||
|
|
||
| def _parse_mcp_servers_from_file(file_path, io, verbose=False, mcp_transport="stdio"): | ||
| """Parse MCP servers from a JSON file.""" | ||
| # Resolve the file path relative to closest cecli.conf.yml, git directory, or CWD | ||
| resolved_file_path = _resolve_mcp_config_path(file_path, io, verbose) | ||
|
|
||
| try: | ||
| with open(resolved_file_path, "r") as f: | ||
| json_string = f.read() | ||
|
|
||
| if verbose: | ||
| io.tool_output(f"Loading MCP servers from file: {file_path}") | ||
|
|
||
| return _parse_mcp_servers_from_json_string(json_string, io, verbose, mcp_transport) | ||
|
|
||
| except FileNotFoundError: | ||
| io.tool_warning(f"MCP config file not found: {file_path}") | ||
| except Exception as e: | ||
| io.tool_error(f"Error reading MCP config file: {e}") | ||
|
|
||
| return [] | ||
|
|
||
|
|
||
| def load_mcp_servers(mcp_servers, mcp_servers_file, io, verbose=False, mcp_transport="stdio"): | ||
| """Load MCP servers from a JSON string or file.""" | ||
| servers = [] | ||
|
|
||
| # First try to load from the JSON string (preferred) | ||
| if mcp_servers: | ||
| servers = _parse_mcp_servers_from_json_string(mcp_servers, io, verbose, mcp_transport) | ||
| if servers: | ||
| return servers | ||
|
|
||
| # If JSON string failed or wasn't provided, try the file | ||
| if mcp_servers_file: | ||
| servers = _parse_mcp_servers_from_file(mcp_servers_file, io, verbose, mcp_transport) | ||
|
|
||
| if not servers: | ||
| # A default MCP server is actually now necessary for the overall agentic loop | ||
| # and a dummy server does suffice for the job | ||
| # because I am not smart enough to figure out why | ||
| # on coder switch, the agent actually initializes the prompt area twice | ||
| # once immediately after input for the old coder | ||
| # and immediately again for the new target coder | ||
| # which causes a race condition where we are awaiting a coroutine | ||
| # that can no longer yield control (somehow?) | ||
| # but somehow having to run through the MCP server checks | ||
| # allows control to be yielded again somehow | ||
| # and I cannot figure out just how that is happening | ||
| # and maybe it is actually prompt_toolkit's fault | ||
| # but this hack works swimmingly because ??? | ||
| # so sure! why not | ||
| servers = [McpServer(json.loads('{"cecli_default": {}}'), io=io, verbose=verbose)] | ||
|
|
||
| return servers | ||
| from .manager import McpServerManager | ||
| from .server import HttpStreamingServer, LocalServer, McpServer, SseServer | ||
| from .utils import find_available_port, generate_pkce_codes, load_mcp_servers | ||
|
|
||
| __all__ = [ | ||
| "McpServerManager", | ||
| "McpServer", | ||
| "HttpStreamingServer", | ||
| "SseServer", | ||
| "LocalServer", | ||
| "load_mcp_servers", | ||
| "find_available_port", | ||
| "generate_pkce_codes", | ||
| ] |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Still valid since this is a minimal PR to get the codebase using the mcp manager