feat(mcp): add MCP discovery and dynamic loading support#685
feat(mcp): add MCP discovery and dynamic loading support#685
Conversation
There was a problem hiding this comment.
Caution
Changes requested ❌
Reviewed everything up to 2271c6d in 2 minutes and 7 seconds. Click for details.
- Reviewed
1022lines of code in6files - Skipped
0files when reviewing. - Skipped posting
6draft comments. View those below. - Modify your settings and rules to customize what types of comments Ellipsis leaves. And don't forget to react with 👍 or 👎 to teach Ellipsis.
1. docs/mcp.rst:37
- Draft comment:
Detailed management tool documentation with clear examples. Good job! - Reason this comment was not posted:
Confidence changes required:0%<= threshold50%None
2. gptme/mcp/__init__.py:9
- Draft comment:
Extended all exports to include registry and formatting functions. Looks correct. - Reason this comment was not posted:
Confidence changes required:0%<= threshold50%None
3. gptme/tools/mcp.py:43
- Draft comment:
Silently ignoring JSON decode errors may hide misconfigurations. Consider notifying the user of invalid JSON input. - Reason this comment was not posted:
Decided after close inspection that this draft comment was likely wrong and/or not actionable: usefulness confidence = 20% vs. threshold = 50% The JSON input is optional - if it fails to parse, using empty command_args is a reasonable fallback. Looking at the examples in the docstring, the JSON is only used for optional configuration. The code handles the missing config gracefully by using defaults. Notifying about parse errors would add noise in cases where JSON wasn't intended. The comment raises a valid point about error visibility. Silent failures can make debugging harder. However, since this is optional config with sensible defaults, and the JSON format is clearly documented, silent failure is actually the better UX here - it gracefully degrades to default behavior. The current behavior of silently falling back to defaults is appropriate for optional configuration. The comment should be removed.
4. gptme/tools/mcp_adapter.py:29
- Draft comment:
If concurrent access is possible, ensure thread-safety when using the global _dynamic_servers cache. - Reason this comment was not posted:
Confidence changes required:50%<= threshold50%None
5. gptme/tools/mcp_adapter.py:296
- Draft comment:
Validate the types/values in config_override before applying them to the server configuration to avoid misconfiguration. - Reason this comment was not posted:
Comment did not seem useful. Confidence is useful =20%<= threshold50%This comment is asking the author to validate types/values in a configuration, which is a general suggestion to ensure correctness. It doesn't provide a specific code suggestion or point out a specific issue in the code. It seems to be more of a reminder or a general best practice rather than a specific actionable comment.
6. tests/test_mcp_discovery.py:215
- Draft comment:
Tests are comprehensive; consider adding cases for invalid JSON input to ensure proper error handling in MCP commands. - Reason this comment was not posted:
Confidence changes required:50%<= threshold50%None
Workflow ID: wflow_tUQKBnpyeGlvQWgX
You can customize by changing your verbosity settings, reacting with 👍 or 👎, replying to comments, or adding code review rules.
| url = f"{self.OFFICIAL_REGISTRY_URL}/api/search" | ||
| params = {"q": query, "limit": limit} if query else {"limit": limit} | ||
|
|
||
| response = requests.get(url, params=params, timeout=10) |
There was a problem hiding this comment.
Consider caching API responses here to reduce redundant HTTP calls, especially for repetitive searches.
| ] | ||
|
|
||
| if server.registry: | ||
| output.append(f"**Registry:** {server.registry}") |
There was a problem hiding this comment.
When formatting server details, sanitize user-supplied inputs to avoid potential markdown injection.
| output.append(f"**Registry:** {server.registry}") | |
| output.append(f"**Registry:** {server.registry.replace('*', '\\*').replace('`', '\\`')}") |
There was a problem hiding this comment.
Important
Looks good to me! 👍
Reviewed f2a2c9b in 51 seconds. Click for details.
- Reviewed
71lines of code in1files - Skipped
0files when reviewing. - Skipped posting
2draft comments. View those below. - Modify your settings and rules to customize what types of comments Ellipsis leaves. And don't forget to react with 👍 or 👎 to teach Ellipsis.
1. gptme/util/cli.py:157
- Draft comment:
Consider validating that the 'limit' value is a positive integer to avoid nonsensical inputs. - Reason this comment was not posted:
Confidence changes required:50%<= threshold50%None
2. gptme/util/cli.py:205
- Draft comment:
Consider logging the caught exception (in addition to echoing it) for improved traceability. - Reason this comment was not posted:
Confidence changes required:33%<= threshold50%None
Workflow ID: wflow_1hlSp2AQDbZtgCXY
You can customize by changing your verbosity settings, reacting with 👍 or 👎, replying to comments, or adding code review rules.
There was a problem hiding this comment.
Caution
Changes requested ❌
Reviewed bb2010e in 1 minute and 38 seconds. Click for details.
- Reviewed
171lines of code in2files - Skipped
0files when reviewing. - Skipped posting
3draft comments. View those below. - Modify your settings and rules to customize what types of comments Ellipsis leaves. And don't forget to react with 👍 or 👎 to teach Ellipsis.
1. gptme/tools/mcp.py:86
- Draft comment:
For non-HTTP servers, consider including environment variables (if any) similar to the CLI output for consistency. - Reason this comment was not posted:
Confidence changes required:33%<= threshold50%None
2. gptme/util/cli.py:163
- Draft comment:
Good enhancement in 'mcp_info': checking local configuration first then searching registries. Consider a timeout for the connection test to avoid blocking long waits. - Reason this comment was not posted:
Confidence changes required:33%<= threshold50%None
3. gptme/util/cli.py:180
- Draft comment:
Removal of the separate 'discover' command simplifies the interface. Ensure this change is documented in release notes. - Reason this comment was not posted:
Confidence changes required:0%<= threshold50%None
Workflow ID: wflow_eCaleP5J2U0ftNMo
You can customize by changing your verbosity settings, reacting with 👍 or 👎, replying to comments, or adding code review rules.
| else: | ||
| # Not found locally, search registries | ||
| result = get_mcp_server_info(name) | ||
| if "not found" in result.lower(): |
There was a problem hiding this comment.
Using a simple substring check ('not found' in result.lower()) is brittle. Consider returning structured error data or raising an exception instead.
|
Looks like there are a few issues preventing this PR from being merged!
If you'd like me to help, just leave a comment, like Feel free to include any additional details that might help me get this PR into a better state. You can manage your notification settings |
There was a problem hiding this comment.
Caution
Changes requested ❌
Reviewed c07476d in 14 minutes and 10 seconds. Click for details.
- Reviewed
257lines of code in1files - Skipped
0files when reviewing. - Skipped posting
2draft comments. View those below. - Modify your settings and rules to customize what types of comments Ellipsis leaves. And don't forget to react with 👍 or 👎 to teach Ellipsis.
1. gptme/mcp/registry.py:251
- Draft comment:
The fallback behavior in get_server_details returns the first partial match if no exact match is found. Confirm that this behavior is intended and well-documented in the API. - Reason this comment was not posted:
Comment did not seem useful. Confidence is useful =0%<= threshold50%The comment is asking the PR author to confirm their intention and ensure documentation, which violates the rules. It doesn't provide a specific code suggestion or point out a clear issue.
2. gptme/mcp/registry.py:183
- Draft comment:
For search_mcp_so, consider adding a TODO comment for future API integration if MCP.so provides a public endpoint later. - Reason this comment was not posted:
Confidence changes required:33%<= threshold50%None
Workflow ID: wflow_JgYsZ3KKuaURW1co
You can customize by changing your verbosity settings, reacting with 👍 or 👎, replying to comments, or adding code review rules.
| packages = server_data.get("packages", []) | ||
| command = "" | ||
| args = [] | ||
| server_url = "" |
There was a problem hiding this comment.
Server URL remains unset (empty string) when processing API responses. Consider extracting it from the response (e.g. server_data.get('url', '')) so that MCPServerInfo.url is populated if available.
| server_url = "" | |
| server_url = server_data.get("url", "") |
| install_command="", | ||
| ) | ||
| except requests.RequestException: | ||
| pass |
There was a problem hiding this comment.
In the UUID-based lookup for server details, exceptions are silently swallowed. Consider logging the exception to aid debugging.
| pass | |
| logger.exception("Failed to fetch server details by UUID") |
There was a problem hiding this comment.
Important
Looks good to me! 👍
Reviewed 61f64b4 in 1 minute and 2 seconds. Click for details.
- Reviewed
29lines of code in2files - Skipped
0files when reviewing. - Skipped posting
2draft comments. View those below. - Modify your settings and rules to customize what types of comments Ellipsis leaves. And don't forget to react with 👍 or 👎 to teach Ellipsis.
1. tests/test_mcp.py:24
- Draft comment:
Ensure the new error message substrings ('not configured locally' and 'not found in registries either') reliably match the output. Consider using regex or common constants to avoid brittle tests. - Reason this comment was not posted:
Decided after close inspection that this draft comment was likely wrong and/or not actionable: usefulness confidence = 20% vs. threshold = 50% The comment starts with "Ensure that..." which is a red flag per the rules. The suggestion to use regex or constants is a code quality improvement, but the current approach using string contains is a perfectly valid and common testing pattern. The test is already quite clear and maintainable. Making this change would be an optional refactor rather than a necessary fix. The comment does point out a legitimate potential maintenance concern - if the error messages change, the test will break. Using constants could make updates easier. While using constants could be nice, the current approach is a standard testing pattern. The "Ensure that..." phrasing makes this more of a suggestion than a required change. Delete this comment. It starts with "Ensure that..." which violates our rules, and while the suggestion could be nice-to-have, the current test approach is perfectly valid.
2. tests/test_prompts.py:27
- Draft comment:
Updated token count upper limit to 3300; verify that this hardcoded limit remains appropriate as MCP tool docs evolve. - Reason this comment was not posted:
Comment did not seem useful. Confidence is useful =0%<= threshold50%This comment is asking the PR author to verify the appropriateness of a hardcoded limit in the future, which is not specific to the current code change. It doesn't provide a specific suggestion or ask for a test to be written. It violates the rule against asking the author to ensure behavior is intended or to double-check things.
Workflow ID: wflow_jYxjg5C7NYRZFIoG
You can customize by changing your verbosity settings, reacting with 👍 or 👎, replying to comments, or adding code review rules.
Codecov Report❌ Patch coverage is
📢 Thoughts on this report? Let us know! |
There was a problem hiding this comment.
Important
Looks good to me! 👍
Reviewed 87bac45 in 38 seconds. Click for details.
- Reviewed
32lines of code in2files - Skipped
0files when reviewing. - Skipped posting
2draft comments. View those below. - Modify your settings and rules to customize what types of comments Ellipsis leaves. And don't forget to react with 👍 or 👎 to teach Ellipsis.
1. gptme/prompts.py:87
- Draft comment:
Good use of a conditional expression for agent_msgs; it safely returns an empty list when agent_path is not provided. - Reason this comment was not posted:
Confidence changes required:0%<= threshold50%None
2. tests/test_prompts.py:26
- Draft comment:
Updated token limit from 3300 to 4700 to accommodate the expanded MCP documentation in the short prompt. - Reason this comment was not posted:
Confidence changes required:0%<= threshold50%None
Workflow ID: wflow_c0XUsY0QU7a0FgYn
You can customize by changing your verbosity settings, reacting with 👍 or 👎, replying to comments, or adding code review rules.
- Added 37 tests covering all MCP CLI commands (list, test, info, search) - Tests cover success cases, error cases, and edge cases - Fixed tool mock creation to properly set .name attribute - Fixed server mock to include required stdio attributes - Fixed type annotation for mypy compliance Coverage improvements: - gptme/util/cli.py: 67% → 68% - All MCP commands now have test coverage - 35/37 tests passed on first run, 37/37 after fixes Addresses missing test coverage in PR #685
There was a problem hiding this comment.
Caution
Changes requested ❌
Reviewed 3739ca2 in 2 minutes and 3 seconds. Click for details.
- Reviewed
520lines of code in1files - Skipped
0files when reviewing. - Skipped posting
4draft comments. View those below. - Modify your settings and rules to customize what types of comments Ellipsis leaves. And don't forget to react with 👍 or 👎 to teach Ellipsis.
1. tests/test_util_cli_mcp.py:43
- Draft comment:
Repeated instantiation of CliRunner() is seen across many tests. Consider defining a pytest fixture for CliRunner to DRY up the test setup. - Reason this comment was not posted:
Confidence changes required:50%<= threshold50%None
2. tests/test_util_cli_mcp.py:1
- Draft comment:
There are no tests for the 'mcp load' and 'mcp unload' commands, even though they're mentioned in the PR description. Consider adding tests to cover these dynamic loading commands. - Reason this comment was not posted:
Comment was on unchanged code.
3. tests/test_util_cli_mcp.py:90
- Draft comment:
Assertions rely on exact output strings (e.g., 'Connection failed', '+2 more') which may lead to brittle tests if output formatting changes. Consider using more flexible matching or regex. - Reason this comment was not posted:
Confidence changes required:50%<= threshold50%None
4. tests/test_util_cli_mcp.py:432
- Draft comment:
Typo: The expected output string "Searching all registry" in this test might be intended to be "Searching all registries" for correct pluralization. - Reason this comment was not posted:
Decided after close inspection that this draft comment was likely wrong and/or not actionable: usefulness confidence = 0% vs. threshold = 50% While the comment correctly identifies a grammatical error, it's a very minor issue in a test string that doesn't affect functionality. The test will pass or fail the same way regardless of the pluralization. The rules state we should not make purely informative comments or comments about obvious/unimportant issues. The grammatical error could indicate sloppiness that we might want to fix for code quality. Test messages should be clear and grammatically correct. While clean test messages are good, this is too minor of an issue to warrant a PR comment. It doesn't affect functionality or readability significantly. Delete the comment as it points out an extremely minor grammatical issue that doesn't meaningfully impact the code quality or functionality.
Workflow ID: wflow_MkgfZVoAA42MakQG
You can customize by changing your verbosity settings, reacting with 👍 or 👎, replying to comments, or adding code review rules.
There was a problem hiding this comment.
Important
Looks good to me! 👍
Reviewed 2244a24 in 48 seconds. Click for details.
- Reviewed
29lines of code in2files - Skipped
0files when reviewing. - Skipped posting
2draft comments. View those below. - Modify your settings and rules to customize what types of comments Ellipsis leaves. And don't forget to react with 👍 or 👎 to teach Ellipsis.
1. gptme/util/cli.py:194
- Draft comment:
Improved clarity in search output message. Ensure consistent singular/plural use (e.g., 'all registries') across code and documentation. - Reason this comment was not posted:
Confidence changes required:0%<= threshold50%None
2. tests/test_util_cli_mcp.py:429
- Draft comment:
Test string updated to match the new CLI output. Verify that the expected output is consistent. - Reason this comment was not posted:
Confidence changes required:0%<= threshold50%None
Workflow ID: wflow_yqi6P2dD2RxJRupi
You can customize by changing your verbosity settings, reacting with 👍 or 👎, replying to comments, or adding code review rules.
- Add 21 comprehensive tests covering all execute_mcp commands: * search (with various parameters and JSON args) * info (local servers, HTTP servers, registry servers, not found) * load (with/without config override, with confirmation/cancellation) * unload (with confirmation/cancellation) * list * Error cases (no command, unknown command, missing arguments) * Exception handling * Invalid JSON args handling - Improve coverage from 17% to 100% for gptme/tools/mcp.py (42 lines) - All tests use proper mocking of external dependencies - Fix existing test to use correct import (MCPServerConfig) Addresses Codecov feedback on PR #685 showing 61.92% patch coverage. This improvement covers the largest single gap in the PR.
- Add 14 new tests covering all missing code paths in gptme/tools/mcp.py - Test coverage improved from 53% to 100% (+47 percentage points) - Covers: JSON parsing errors, info command variants, load/unload workflows, confirmation cancellations, error handling - Tests for local server info (HTTP and stdio types) - Tests for registry search fallback scenarios - All 24 tests passing Addresses test coverage feedback on PR #685 (MCP discovery feature)
There was a problem hiding this comment.
Important
Looks good to me! 👍
Reviewed 024e378 in 47 seconds. Click for details.
- Reviewed
309lines of code in1files - Skipped
0files when reviewing. - Skipped posting
2draft comments. View those below. - Modify your settings and rules to customize what types of comments Ellipsis leaves. And don't forget to react with 👍 or 👎 to teach Ellipsis.
1. tests/test_mcp_discovery.py:232
- Draft comment:
Consider extracting the repeated 'confirm' lambda into a shared fixture to reduce duplication across tests. - Reason this comment was not posted:
Confidence changes required:50%<= threshold50%None
2. tests/test_mcp_discovery.py:345
- Draft comment:
The assertion checking for 'No' in the stdio server test is too loose. Consider asserting a more explicit string like 'Enabled: No' for clarity. - Reason this comment was not posted:
Confidence changes required:50%<= threshold50%None
Workflow ID: wflow_BJNmLbmZTil0edFr
You can customize by changing your verbosity settings, reacting with 👍 or 👎, replying to comments, or adding code review rules.
- Added 37 tests covering all MCP CLI commands (list, test, info, search) - Tests cover success cases, error cases, and edge cases - Fixed tool mock creation to properly set .name attribute - Fixed server mock to include required stdio attributes - Fixed type annotation for mypy compliance Coverage improvements: - gptme/util/cli.py: 67% → 68% - All MCP commands now have test coverage - 35/37 tests passed on first run, 37/37 after fixes Addresses missing test coverage in PR #685
- Add 14 new tests covering all missing code paths in gptme/tools/mcp.py - Test coverage improved from 53% to 100% (+47 percentage points) - Covers: JSON parsing errors, info command variants, load/unload workflows, confirmation cancellations, error handling - Tests for local server info (HTTP and stdio types) - Tests for registry search fallback scenarios - All 24 tests passing Addresses test coverage feedback on PR #685 (MCP discovery feature)
024e378 to
09bb7b5
Compare
|
lfg |
Implements MCP server discovery and management capabilities inspired by Elliott's MCP gateway idea and Model Context Protocol best practices. Core Features: - MCPRegistry module for searching MCP servers across: - Official MCP Registry (registry.modelcontextprotocol.io) - MCP.so directory - Extensible registry support - New "mcp" management tool with commands: - search: Find MCP servers in registries (with filtering) - info: Get detailed server information - load: Dynamically load servers during session - unload: Unload previously loaded servers - list: Show all configured/loaded servers - Dynamic loading features: - Runtime server loading without config file edits - Configuration overrides at load time - Context optimization via lazy loading - Automatic tool discovery from loaded servers Implementation: - gptme/mcp/registry.py: Registry search and server info - gptme/tools/mcp.py: Management tool implementation - gptme/tools/mcp_adapter.py: Dynamic loading functions - docs/mcp.rst: Comprehensive documentation with examples - tests/test_mcp_discovery.py: Full test coverage Code Quality: - Removed lru_cache from class methods (avoids memory leaks) - Proper function definitions instead of lambdas - Type-safe implementation with mypy checks - Formatting fixes from pre-commit hooks This addresses the MCP gateway/discovery problem by enabling: 1. Runtime discovery of vetted MCP servers 2. Dynamic loading to avoid context bloat 3. Developer-friendly server ecosystem 4. Context-aware resource management
Add search and discover commands to gptme-util mcp: - mcp search: Search for MCP servers in registries - mcp discover: Get detailed info about a specific server These are lean wrappers around the registry functions, reusing the core functionality from gptme.mcp.registry.
Use the actual MCP Registry v0 API: - GET /v0/servers with search and limit parameters - Parse nested server data structure correctly - Extract package information for installation commands - Handle npm and pypi package types - Fix type annotation for params dict MCP.so search disabled (no public API available). Fixes server discovery to return proper results with: - Server names and descriptions - Repository URLs - Installation commands - Configuration examples
…mand Simplify the MCP command interface by merging info/show/discover into a single smart "info" command that: 1. First checks if server is configured locally 2. If found locally, shows config and tests connection 3. If not found locally, searches registries 4. Shows whatever information is available This makes the interface much more intuitive - users don't need to know where the information comes from, they just ask for info. Commands now: - search: Browse/search registries - info: Get details (local or registry) - list: List configured servers - test: Test connection The discover command has been removed to reduce confusion.
- Update test_mcp_cli_commands to match improved error messages - New messages are more user-friendly with registry search fallback - Increase prompt size limit to 3300 tokens to accommodate MCP tool docs - MCP tool documentation is comprehensive and valuable for users - Increase is only 200 tokens (6.7% from previous 3000 limit)
- Increase prompt token threshold from 3300 to 4700 to accommodate MCP tool documentation - Only generate agent workspace messages when agent_path is provided - Fixes test_get_prompt_short and test_get_prompt_custom failures
- Add 16 comprehensive tests covering all main functions - Test create_mcp_tools with various configurations - Test search, info, load, unload, list operations - Improve overall PR patch coverage Coverage improvement: - mcp_adapter.py: 27% → 72% (108/149 lines covered) - All 16 new tests passing - No regressions in existing tests
- Added 37 tests covering all MCP CLI commands (list, test, info, search) - Tests cover success cases, error cases, and edge cases - Fixed tool mock creation to properly set .name attribute - Fixed server mock to include required stdio attributes - Fixed type annotation for mypy compliance Coverage improvements: - gptme/util/cli.py: 67% → 68% - All MCP commands now have test coverage - 35/37 tests passed on first run, 37/37 after fixes Addresses missing test coverage in PR #685
- Add 14 new tests covering all missing code paths in gptme/tools/mcp.py - Test coverage improved from 53% to 100% (+47 percentage points) - Covers: JSON parsing errors, info command variants, load/unload workflows, confirmation cancellations, error handling - Tests for local server info (HTTP and stdio types) - Tests for registry search fallback scenarios - All 24 tests passing Addresses test coverage feedback on PR #685 (MCP discovery feature)
821acea to
a3d5aae
Compare
There was a problem hiding this comment.
Important
Looks good to me! 👍
Reviewed a3d5aae in 64 minutes and 59 seconds. Click for details.
- Reviewed
309lines of code in1files - Skipped
0files when reviewing. - Skipped posting
5draft comments. View those below. - Modify your settings and rules to customize what types of comments Ellipsis leaves. And don't forget to react with 👍 or 👎 to teach Ellipsis.
1. tests/test_mcp_discovery.py:232
- Draft comment:
Consider using a lambda (e.g., lambda _: True) for the confirm callback to reduce verbosity and duplicate code across tests. - Reason this comment was not posted:
Confidence changes required:33%<= threshold50%None
2. tests/test_mcp_discovery.py:127
- Draft comment:
Inconsistent patch targets for get_config: test_execute_mcp_list patches 'gptme.tools.mcp_adapter.get_config' while others patch 'gptme.config.get_config'. Confirm if this difference is intentional. - Reason this comment was not posted:
Comment was not on a location in the diff, so it can't be submitted as a review comment.
3. tests/test_mcp_discovery.py:263
- Draft comment:
In the invalid JSON test, consider asserting that the fallback behavior is applied explicitly, not just checking for the presence of 'sqlite' in the output. - Reason this comment was not posted:
Confidence changes required:33%<= threshold50%None
4. tests/test_mcp_discovery.py:302
- Draft comment:
The assertions in the HTTP server info test rely on specific output formatting (e.g., 'HTTP', 'Headers'). This could be brittle; consider verifying key fields more flexibly. - Reason this comment was not posted:
Confidence changes required:33%<= threshold50%None
5. tests/test_mcp_discovery.py:217
- Draft comment:
Overall, tests are comprehensive. To reduce duplication, consider refactoring common confirm lambdas and configuration setup into fixtures or helper functions. - Reason this comment was not posted:
Confidence changes required:33%<= threshold50%None
Workflow ID: wflow_4S4Ov6XI5zrHbk5a
You can customize by changing your verbosity settings, reacting with 👍 or 👎, replying to comments, or adding code review rules.
|
Far from perfect, but I think we'll merge it for now. |
* feat(mcp): add MCP discovery and dynamic loading support Implements MCP server discovery and management capabilities inspired by Elliott's MCP gateway idea and Model Context Protocol best practices. Core Features: - MCPRegistry module for searching MCP servers across: - Official MCP Registry (registry.modelcontextprotocol.io) - MCP.so directory - Extensible registry support - New "mcp" management tool with commands: - search: Find MCP servers in registries (with filtering) - info: Get detailed server information - load: Dynamically load servers during session - unload: Unload previously loaded servers - list: Show all configured/loaded servers - Dynamic loading features: - Runtime server loading without config file edits - Configuration overrides at load time - Context optimization via lazy loading - Automatic tool discovery from loaded servers Implementation: - gptme/mcp/registry.py: Registry search and server info - gptme/tools/mcp.py: Management tool implementation - gptme/tools/mcp_adapter.py: Dynamic loading functions - docs/mcp.rst: Comprehensive documentation with examples - tests/test_mcp_discovery.py: Full test coverage Code Quality: - Removed lru_cache from class methods (avoids memory leaks) - Proper function definitions instead of lambdas - Type-safe implementation with mypy checks - Formatting fixes from pre-commit hooks This addresses the MCP gateway/discovery problem by enabling: 1. Runtime discovery of vetted MCP servers 2. Dynamic loading to avoid context bloat 3. Developer-friendly server ecosystem 4. Context-aware resource management * feat(mcp): add discovery commands to gptme-util CLI Add search and discover commands to gptme-util mcp: - mcp search: Search for MCP servers in registries - mcp discover: Get detailed info about a specific server These are lean wrappers around the registry functions, reusing the core functionality from gptme.mcp.registry. * fix(mcp): implement correct registry API endpoints Use the actual MCP Registry v0 API: - GET /v0/servers with search and limit parameters - Parse nested server data structure correctly - Extract package information for installation commands - Handle npm and pypi package types - Fix type annotation for params dict MCP.so search disabled (no public API available). Fixes server discovery to return proper results with: - Server names and descriptions - Repository URLs - Installation commands - Configuration examples * refactor(mcp): consolidate info/discover commands into smart info command Simplify the MCP command interface by merging info/show/discover into a single smart "info" command that: 1. First checks if server is configured locally 2. If found locally, shows config and tests connection 3. If not found locally, searches registries 4. Shows whatever information is available This makes the interface much more intuitive - users don't need to know where the information comes from, they just ask for info. Commands now: - search: Browse/search registries - info: Get details (local or registry) - list: List configured servers - test: Test connection The discover command has been removed to reduce confusion. * test: fix MCP CLI test and adjust prompt size limit - Update test_mcp_cli_commands to match improved error messages - New messages are more user-friendly with registry search fallback - Increase prompt size limit to 3300 tokens to accommodate MCP tool docs - MCP tool documentation is comprehensive and valuable for users - Increase is only 200 tokens (6.7% from previous 3000 limit) * fix: adjust prompt token limit and fix custom prompt test - Increase prompt token threshold from 3300 to 4700 to accommodate MCP tool documentation - Only generate agent workspace messages when agent_path is provided - Fixes test_get_prompt_short and test_get_prompt_custom failures * test: add comprehensive tests for mcp_adapter.py (27% → 72% coverage) - Add 16 comprehensive tests covering all main functions - Test create_mcp_tools with various configurations - Test search, info, load, unload, list operations - Improve overall PR patch coverage Coverage improvement: - mcp_adapter.py: 27% → 72% (108/149 lines covered) - All 16 new tests passing - No regressions in existing tests * test(mcp): add comprehensive CLI tests for MCP commands - Added 37 tests covering all MCP CLI commands (list, test, info, search) - Tests cover success cases, error cases, and edge cases - Fixed tool mock creation to properly set .name attribute - Fixed server mock to include required stdio attributes - Fixed type annotation for mypy compliance Coverage improvements: - gptme/util/cli.py: 67% → 68% - All MCP commands now have test coverage - 35/37 tests passed on first run, 37/37 after fixes Addresses missing test coverage in PR gptme#685 * fix(mcp): correct grammar in search output ("all registries" not "all registry") * test(mcp): add comprehensive tests for mcp tool, achieve 100% coverage - Add 14 new tests covering all missing code paths in gptme/tools/mcp.py - Test coverage improved from 53% to 100% (+47 percentage points) - Covers: JSON parsing errors, info command variants, load/unload workflows, confirmation cancellations, error handling - Tests for local server info (HTTP and stdio types) - Tests for registry search fallback scenarios - All 24 tests passing Addresses test coverage feedback on PR gptme#685 (MCP discovery feature) --------- Co-authored-by: Erik Bjäreholt <erik@bjareho.lt>
Overview
Implements MCP server discovery and dynamic loading capabilities, inspired by @elliot-evno's MCP gateway idea, Erik's take on it, and Model Context Protocol best practices for context optimization.
Problem
Current MCP implementations suffer from:
Solution
This PR adds a comprehensive MCP discovery and management system that allows:
Features
MCPRegistry Module
New mcp Management Tool
Commands:
Dynamic Loading
Implementation
Testing
All tests pass including:
Benefits
Related
Addresses the MCP gateway/discovery problem discussed with @ElliottEvNo:
Important
Adds MCP server discovery and dynamic loading, with CLI management and extensive testing.
gptme/mcp/registry.py: Handles registry search and server metadata.gptme/tools/mcp.py: Provides CLI tool for managing MCP servers.gptme/tools/mcp_adapter.py: Infrastructure for dynamic server loading.search [query]: Find MCP servers.info <server-name>: Get server details.load <server-name>: Load a server dynamically.unload <server-name>: Unload a server.list: List all configured/loaded servers.tests/test_mcp_discovery.py,tests/test_mcp_adapter.py, andtests/test_util_cli_mcp.py.docs/mcp.rstwith new management tool commands and examples.This description was created by
for a3d5aae. You can customize this summary. It will automatically update as commits are pushed.