-
Notifications
You must be signed in to change notification settings - Fork 33
feat(agentgateway): enhance credential detection logic for service bi… #170
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
Closed
Closed
Changes from all commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
1c7d799
feat(agentgateway): enhance credential detection logic for service bi…
KoblerS 9e029a6
fix(agentgateway): update credential detection logic to scan for serv…
KoblerS 3d4fc6b
Refactor MCP session management and update error handling
KoblerS 1364eee
Merge branch 'main' into main
KoblerS 31b37cc
fix(mcp_session): handle null results and log warnings for MCP tool c…
KoblerS 8412fdb
Merge branch 'main' of https://github.com/KoblerS/cloud-sdk-python
KoblerS fcd1ebc
feat: enhance MCP tool handling with ORD ID filtering and improve doc…
KoblerS ad9a7fb
fix: resolve base mount for SERVICE_BINDING_ROOT and clean up imports
KoblerS 0db7285
Merge upstream main — resolve conflicts and integrate extensibility/a…
KoblerS 6ef3f18
fix: resolve CI failures after upstream merge
KoblerS a809fd1
fix(tests): update agentgateway unit tests after upstream merge
KoblerS 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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,93 @@ | ||
| """Shared helpers for MCP session management.""" | ||
|
|
||
| import logging | ||
| import uuid | ||
|
|
||
| import httpx | ||
| from mcp import ClientSession, McpError | ||
| from mcp.client.streamable_http import streamable_http_client | ||
|
|
||
| from sap_cloud_sdk.agentgateway._models import MCPTool | ||
| from sap_cloud_sdk.agentgateway.exceptions import AgentGatewayServerError | ||
|
|
||
| logger = logging.getLogger(__name__) | ||
|
|
||
|
|
||
| async def invoke_mcp_tool( | ||
| tool: MCPTool, auth_token: str, timeout: float, **kwargs | ||
| ) -> str: | ||
| """Open an MCP session, call a tool, and return its text result. | ||
|
|
||
| Handles McpError from both initialize() and call_tool(), and checks | ||
| result.isError, raising AgentGatewayServerError in all three cases. | ||
|
|
||
| Args: | ||
| tool: MCPTool to invoke. | ||
| auth_token: Raw bearer token for the Authorization header. | ||
| timeout: HTTP timeout in seconds. | ||
| **kwargs: Tool input parameters forwarded to call_tool(). | ||
|
|
||
| Returns: | ||
| Tool result as a string, or "" if the response has no content. | ||
|
|
||
| Raises: | ||
| AgentGatewayServerError: If the server returns any kind of error. | ||
| """ | ||
| async with httpx.AsyncClient( | ||
| headers={ | ||
| "Authorization": f"Bearer {auth_token}", | ||
| "x-correlation-id": str(uuid.uuid4()), | ||
| }, | ||
| timeout=timeout, | ||
| ) as http_client: | ||
| try: | ||
| async with streamable_http_client(tool.url, http_client=http_client) as ( | ||
| read, | ||
| write, | ||
| _, | ||
| ): | ||
| async with ClientSession(read, write) as session: | ||
| try: | ||
| await session.initialize() | ||
| except McpError as e: | ||
| raise AgentGatewayServerError( | ||
| f"Agent Gateway rejected MCP session for tool '{tool.name}': {e.error.message}", | ||
| error_code=e.error.code, | ||
| ) from e | ||
| try: | ||
| result = await session.call_tool(tool.name, kwargs) | ||
| except McpError as e: | ||
| raise AgentGatewayServerError( | ||
| f"Agent Gateway returned error for tool '{tool.name}': {e.error.message}", | ||
| error_code=e.error.code, | ||
| ) from e | ||
| if result is None: | ||
| logger.warning("Tool '%s' returned a null result", tool.name) | ||
| return "" | ||
| if result.isError: | ||
| raise AgentGatewayServerError( | ||
| f"Tool '{tool.name}' returned an error: {_error_text(result.content)}" | ||
| ) | ||
| if not result.content: | ||
| return "" | ||
| return str(getattr(result.content[0], "text", "")) | ||
| except BaseExceptionGroup as eg: | ||
| # anyio wraps task-group exceptions into ExceptionGroups. If the only | ||
| # leaf exception is AttributeError it means an older MCP library version | ||
| # crashed on a null result body inside call_tool. Re-raise anything else. | ||
| attr_errors, rest = eg.split(AttributeError) | ||
| if rest is not None: | ||
| raise | ||
| logger.warning( | ||
| "Tool '%s' returned a null result (MCP null-result bug: %s)", | ||
| tool.name, | ||
| attr_errors, | ||
| ) | ||
| return "" | ||
|
|
||
|
|
||
| def _error_text(content: list) -> str: | ||
| """Extract a human-readable message from MCP error content blocks.""" | ||
| texts = [getattr(block, "text", None) for block in content] | ||
| message = " ".join(t for t in texts if t) | ||
| return message or "unknown error" | ||
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.
How s this different from run_mcp_tool?
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.
could you point me to run_mcp_tool please, not sure where this comes?
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.
sorry, it's call_mcp_tool: https://github.com/SAP/cloud-sdk-python/blob/main/src/sap_cloud_sdk/agentgateway/agw_client.py