Skip to content

Add httpx.HTTPStatusError error handling to tool run #1119

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

Open
wants to merge 2 commits into
base: main
Choose a base branch
from

Conversation

tmatthews90
Copy link

@tmatthews90 tmatthews90 commented Jul 10, 2025

This change adds httpx.HTTPStatusError handling to the tool run class to provide detailed error information when returning from the MCP server.

Motivation and Context

The default error message from httpx does not have detailed information on the exception thrown. Our tools use httpx to make API calls and it would be much easier for us to raise_for_status and not have to catch the exception to format a better message to be rasied.

Current tool implementation example

This is currently what we have to do to get a detailed exception message to the mcp tool run handler.

async def list_memories(page: int = 1, limit: int = 10) -> str:
    """Retrieves a list of memories."""
    token = get_token()
    headers = {"Authorization": token}

    api_url = get_api_url()
    memory_collection_id = get_memory_collection_id()

    async with httpx.AsyncClient(timeout=30, verify=False) as client:
        try:
            params = {
                "collection_id": memory_collection_id,
                "page": page,
                "limit": limit,
            }
            response = await client.get(
                f"{api_url}/memory/{memory_collection_id}/memories",
                params=params,
                headers=headers,
            )
            response.raise_for_status()
            return response.json()
        except httpx.HTTPStatusError as e:
            try:
                error_detail = e.response.json()
            except:
                error_detail = e.response.text
            raise Exception(error_detail)

Ideal tool implementation

@mcp.tool()
async def list_memories(page: int = 1, limit: int = 10) -> str:
    """Retrieves a list of memories."""
    token = get_token()
    headers = {"Authorization": token}

    api_url = get_api_url()
    memory_collection_id = get_memory_collection_id()

    async with httpx.AsyncClient(timeout=30, verify=False) as client:
        params = {
            "collection_id": memory_collection_id,
            "page": page,
            "limit": limit,
        }
        response = await client.get(
            f"{api_url}/memory/{memory_collection_id}/memories",
            params=params,
            headers=headers,
        )
        response.raise_for_status()
        return response.json()

Current response format

Error executing tool list_memories: 404 Client Error: Not Found for url: https://httpbin.org/status/404 For more information check: https://developer.mozilla.org/en-US/docs/Web/HTTP/Status/404

Ideal response

Error executing tool list_memories: [404] <JSON response | text from response>

How Has This Been Tested?

Breaking Changes

Types of changes

  • Bug fix (non-breaking change which fixes an issue)
  • New feature (non-breaking change which adds functionality)
  • Breaking change (fix or feature that would cause existing functionality to change)
  • Documentation update

Checklist

  • I have read the MCP Documentation
  • My code follows the repository's style guidelines
  • New and existing tests pass locally
  • I have added appropriate error handling
  • I have added or updated documentation as needed

Additional context

@tmatthews90 tmatthews90 marked this pull request as ready for review July 10, 2025 16:40
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy