Skip to content
Closed
Show file tree
Hide file tree
Changes from 1 commit
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
4 changes: 2 additions & 2 deletions src/mcp/client/auth/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -241,7 +241,7 @@ async def handle_registration_response(response: Response) -> OAuthClientInforma
client_info = OAuthClientInformationFull.model_validate_json(content)
return client_info
except ValidationError as e: # pragma: no cover
raise OAuthRegistrationError(f"Invalid registration response: {e}")
raise OAuthRegistrationError(f"Invalid registration response: {e}") from e


def is_valid_client_metadata_url(url: str | None) -> bool:
Expand Down Expand Up @@ -334,4 +334,4 @@ async def handle_token_response_scopes(
token_response = OAuthToken.model_validate_json(content)
return token_response
except ValidationError as e: # pragma: no cover
raise OAuthTokenError(f"Invalid token response: {e}")
raise OAuthTokenError(f"Invalid token response: {e}") from e
4 changes: 2 additions & 2 deletions src/mcp/client/session.py
Original file line number Diff line number Diff line change
Expand Up @@ -316,9 +316,9 @@ async def _validate_tool_result(self, name: str, result: types.CallToolResult) -
try:
validate(result.structured_content, output_schema)
except ValidationError as e:
raise RuntimeError(f"Invalid structured content returned by tool {name}: {e}")
raise RuntimeError(f"Invalid structured content returned by tool {name}: {e}") from e
except SchemaError as e: # pragma: no cover
raise RuntimeError(f"Invalid schema for tool {name}: {e}") # pragma: no cover
raise RuntimeError(f"Invalid schema for tool {name}: {e}") from e # pragma: no cover

async def list_prompts(self, *, params: types.PaginatedRequestParams | None = None) -> types.ListPromptsResult:
"""Send a prompts/list request.
Expand Down
2 changes: 1 addition & 1 deletion src/mcp/server/mcpserver/prompts/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -186,4 +186,4 @@ async def render(

return messages
except Exception as e:
raise ValueError(f"Error rendering prompt {self.name}: {e}")
raise ValueError(f"Error rendering prompt {self.name}: {e}") from e
2 changes: 1 addition & 1 deletion src/mcp/server/mcpserver/resources/resource_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ async def get_resource(self, uri: AnyUrl | str, context: Context[LifespanContext
try:
return await template.create_resource(uri_str, params, context=context)
except Exception as e: # pragma: no cover
raise ValueError(f"Error creating resource from template: {e}")
raise ValueError(f"Error creating resource from template: {e}") from e

raise ValueError(f"Unknown resource: {uri}")

Expand Down
2 changes: 1 addition & 1 deletion src/mcp/server/mcpserver/resources/templates.py
Original file line number Diff line number Diff line change
Expand Up @@ -130,4 +130,4 @@ async def create_resource(
fn=lambda: result, # Capture result in closure
)
except Exception as e:
raise ValueError(f"Error creating resource from template: {e}")
raise ValueError(f"Error creating resource from template: {e}") from e
8 changes: 4 additions & 4 deletions src/mcp/server/mcpserver/resources/types.py
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ async def read(self) -> str | bytes:
else:
return pydantic_core.to_json(result, fallback=str, indent=2).decode()
except Exception as e:
raise ValueError(f"Error reading resource {self.uri}: {e}")
raise ValueError(f"Error reading resource {self.uri}: {e}") from e

@classmethod
def from_function(
Expand Down Expand Up @@ -148,7 +148,7 @@ async def read(self) -> str | bytes:
return await anyio.to_thread.run_sync(self.path.read_bytes)
return await anyio.to_thread.run_sync(self.path.read_text)
except Exception as e:
raise ValueError(f"Error reading file {self.path}: {e}")
raise ValueError(f"Error reading file {self.path}: {e}") from e


class HttpResource(Resource):
Expand Down Expand Up @@ -193,7 +193,7 @@ def list_files(self) -> list[Path]: # pragma: no cover
return list(self.path.glob(self.pattern)) if not self.recursive else list(self.path.rglob(self.pattern))
return list(self.path.glob("*")) if not self.recursive else list(self.path.rglob("*"))
except Exception as e:
raise ValueError(f"Error listing directory {self.path}: {e}")
raise ValueError(f"Error listing directory {self.path}: {e}") from e

async def read(self) -> str: # Always returns JSON string # pragma: no cover
"""Read the directory listing."""
Expand All @@ -202,4 +202,4 @@ async def read(self) -> str: # Always returns JSON string # pragma: no cover
file_list = [str(f.relative_to(self.path)) for f in files if f.is_file()]
return json.dumps({"files": file_list}, indent=2)
except Exception as e:
raise ValueError(f"Error reading directory {self.path}: {e}")
raise ValueError(f"Error reading directory {self.path}: {e}") from e
Loading