Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
19 changes: 14 additions & 5 deletions src/mcp_optimizer/toolhive/toolhive_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -149,13 +149,22 @@ def _discover_port(self, port: int | None = None) -> None:
asyncio.run(self._discover_port_async(port))

def _parse_toolhive_version(self, version_str: str) -> Version:
"""Parse ToolHive version string into a Version object."""
"""Parse ToolHive version string into a Version object.

For development/build versions that don't follow SemVer, returns a default version.
"""
try:
version = Version.parse(version_str.replace("v", ""))
return version
except (ValueError, TypeError) as e:
logger.warning("Invalid semver version string", version=version_str, error=str(e))
raise ToolhiveScanError(f"Invalid ToolHive version string: {version_str}") from e
except (ValueError, TypeError):
# For development builds like "build-7c3a3077", use a default version
# This allows the connection to succeed while still logging the issue
logger.info(
"Using default version for non-semver ToolHive build",
version=version_str,
default_version="0.0.0-dev"
)
return Version.parse("0.0.0-dev")

async def _is_toolhive_available(self, host: str, port: int) -> tuple[Version, int]:
"""
Expand All @@ -165,7 +174,7 @@ async def _is_toolhive_available(self, host: str, port: int) -> tuple[Version, i
Tuple of (Version object, port) if available, raises ToolhiveScanError otherwise
"""
try:
async with httpx.AsyncClient(timeout=1.0) as client:
async with httpx.AsyncClient(timeout=self.timeout) as client:
response = await client.get(f"http://{host}:{port}/api/v1beta/version")
response.raise_for_status()

Expand Down
1 change: 1 addition & 0 deletions tests/test_toolhive_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -356,6 +356,7 @@ async def test_is_toolhive_available_validates_response_format(monkeypatch): #
"""
client = ToolhiveClient.__new__(ToolhiveClient)
client.thv_host = "localhost"
client.timeout = 5 # Set the timeout attribute that _is_toolhive_available needs

# Test case 1: Service returns 200 OK but response has no 'version' field
mock_response_wrong_format = Mock()
Expand Down