Skip to content

feat: Add MCP server configuration parsing (e.g.: mcp.json) to the SDK #968

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

Draft
wants to merge 42 commits into
base: main
Choose a base branch
from
Draft
Changes from 1 commit
Commits
Show all changes
42 commits
Select commit Hold shift + click to select a range
7d20254
First cut at config API
msabramo Jun 16, 2025
c92c12f
Add effective_command and effective_args
msabramo Jun 16, 2025
444960c
Make infer_server_types typed; fix warnings
msabramo Jun 16, 2025
2bf423d
test_explicit_types_are_respected
msabramo Jun 16, 2025
8ce1221
More descriptive server names in tests
msabramo Jun 16, 2025
8dfa25b
test_streamable_http_server => test_streamable_http_server_with_headers
msabramo Jun 16, 2025
25b3a7b
Test refactoring
msabramo Jun 16, 2025
37a6bb0
Move test fixtures from Python to mcp.json
msabramo Jun 16, 2025
7d63c26
ruff format
msabramo Jun 16, 2025
38eff49
Add JSONC support
msabramo Jun 16, 2025
71adf90
Revert "Add JSONC support"
msabramo Jun 16, 2025
68b1a2e
Add YAML support
msabramo Jun 16, 2025
a707aeb
Use shlex.split to parse command
msabramo Jun 16, 2025
729c0db
More YAML tests
msabramo Jun 16, 2025
651d504
Handle multiline YAML commands with backslashes
msabramo Jun 16, 2025
54d173e
Fix pre-commit failures
msabramo Jun 16, 2025
76ee90b
ruff format tests/client/config/test_yaml_functionality.py
msabramo Jun 16, 2025
e2e16f4
ruff format tests/client/config/__init__.py
msabramo Jun 16, 2025
8b3593c
Remove unnecessary `import json`
msabramo Jun 16, 2025
f9d48d6
Allow config_path: Path | str
msabramo Jun 16, 2025
4af7fe4
Allow config_path to use ~ for home dir
msabramo Jun 16, 2025
6574cfc
Expand environment variables in config_path
msabramo Jun 16, 2025
dd7a77d
Add as_dict method
msabramo Jun 16, 2025
66e0e0e
Support both "servers" and "mcpServers" keys
msabramo Jun 16, 2025
8c8e657
Support VS Code "inputs" key
msabramo Jun 16, 2025
85036e0
Strip out // comments (JSONC support)
msabramo Jun 16, 2025
27e5b12
Fix lint issues
msabramo Jun 16, 2025
70e91b9
Add tests for when yaml is not importable
msabramo Jun 16, 2025
cf2b2be
Add support for name, description, isActive optional fields
msabramo Jun 16, 2025
6493c57
Smarter detection of SSE servers
msabramo Jun 16, 2025
27c63e6
Lowercase fields before checking for "sse" in them
msabramo Jun 16, 2025
d5aeca6
Remove as_dict method
msabramo Jun 16, 2025
d6d3809
ruff format tests/client/config/test_mcp_servers_config.py
msabramo Jun 16, 2025
fbfa594
Add docs
msabramo Jun 16, 2025
eec3c2b
Documentation tweaks
msabramo Jun 17, 2025
31eb3e1
Docs tweaks
msabramo Jun 17, 2025
9753da8
Docs tweaks
msabramo Jun 17, 2025
02fc66f
Docs tweaks
msabramo Jun 17, 2025
47e92d7
Move input substitution to new `server` method
msabramo Jun 17, 2025
71e20d1
Doc updates
msabramo Jun 17, 2025
0c3bde3
Emit warning when `servers` attribute accessed
msabramo Jun 17, 2025
1ac383e
Add tests/client/config/test_warning_functionality.py
msabramo Jun 17, 2025
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Add tests for when yaml is not importable
  • Loading branch information
msabramo committed Jun 16, 2025
commit 70e91b9a6bd954cf4941273b44cda7a9d19a7997
39 changes: 39 additions & 0 deletions tests/client/config/test_yaml_functionality.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
# stdlib imports
from pathlib import Path
from unittest.mock import patch

# third party imports
import pytest
Expand Down Expand Up @@ -109,3 +110,41 @@ def test_npx_filesystem_server(mcp_yaml_config_file: Path):
"/Users/username/Desktop",
"/path/to/other/allowed/dir",
]


def test_yaml_not_importable_error(mcp_yaml_config_file: Path):
"""Test that trying to parse a YAML file when yaml module is not available raises ImportError."""

# Mock the yaml module to be None (simulating import failure)
with patch("mcp.client.config.mcp_servers_config.yaml", None):
with pytest.raises(ImportError, match="PyYAML is required to parse YAML files"):
MCPServersConfig.from_file(mcp_yaml_config_file)


def test_yaml_not_importable_error_with_use_pyyaml_true():
"""Test that trying to use use_pyyaml=True when yaml module is not available raises ImportError."""

# Create a simple JSON file content but force YAML parsing
json_file = Path(__file__).parent / "mcp.json"

# Mock the yaml module to be None (simulating import failure)
with patch("mcp.client.config.mcp_servers_config.yaml", None):
with pytest.raises(ImportError, match="PyYAML is required to parse YAML files"):
MCPServersConfig.from_file(json_file, use_pyyaml=True)


def test_yaml_not_importable_error_with_yml_extension(tmp_path: Path):
"""Test that trying to parse a .yml file when yaml module is not available raises ImportError."""

# Create a temporary .yml file
yml_file = tmp_path / "test_config.yml"
yml_file.write_text("""
mcpServers:
test_server:
command: python -m test_server
""")

# Mock the yaml module to be None (simulating import failure)
with patch("mcp.client.config.mcp_servers_config.yaml", None):
with pytest.raises(ImportError, match="PyYAML is required to parse YAML files"):
MCPServersConfig.from_file(yml_file)