Skip to content

Using in Python with mcp streamable_http_client #598

Open
@coderdba

Description

@coderdba

I have the following Python client code, but I keep getting 400 - Client error '400 Bad Request' for url 'https://api.githubcopilot.com/mcp/'

What is the fix for this? (NOTE: I tried many request bodies, none worked. They are commented for reference)

I think the error is happening in this line:
client-native-http.py", line 16, in main | async with streamablehttp_client(

Question:

  1. Is the issue in the step to create streamablehttp_client
  2. Further, what is the right way to call the client - that is, the line result = await session.call_tool ...

Code:

# Based on https://github.com/sooperset/mcp-atlassian?tab=readme-ov-file
# Issue: https://github.com/github/github-mcp-server/issues/598

import asyncio
import os

from mcp.client.streamable_http import streamablehttp_client
from mcp import ClientSession

async def main():
    
    # Bearer token from environment variable or configuration
    bearer_token=os.getenv("GITHUB_PAT")
 
    # Connect to streamable HTTP server with custom headers
    async with streamablehttp_client(
        "https://api.githubcopilot.com/mcp/",
        headers={
            "Authorization": f"Bearer {bearer_token}"
        }
    ) as (read_stream, write_stream, _):
        
        print("\n\nINFO - Main: In streamablehttp_client part.")
        # Create a session using the client streams
        async with ClientSession(read_stream, write_stream) as session:
            # Initialize the connection
            await session.initialize()

            print("\n\nINFO - Main: session is: ", session)
            
            # Get-me 
            result = await session.call_tool({
                "name": "get_me",
                "arguments": {"reason": "for information about the user"}
            })
            
            '''
            result = await session.call_tool(
                "get_me",
                {"reason": "for information about the user"}
            )
            '''
            
            '''
            result = await session.call_tool(
                name="get_me",
                arguments={"reason": "for information about the user"}
            )
            '''

            '''
            result = await session.call_tool(
                #"list_branches",
                "get_repo",
                {"message": "list my git repos"},
                #{"owner": "coderdba", "repo": "NOTES"}
            )
            '''
            
            print(result)

asyncio.run(main())

Errors:

python client-native-http.py


INFO - Main: In streamablehttp_client part.
  + Exception Group Traceback (most recent call last):
  |   File "C:\mcp-poc01\mcp-clients\github-official\client-native-http.py", line 62, in <module>
  |     asyncio.run(main())
  |   File "C:\Users\GSM078\AppData\Local\Programs\Python\Python311\Lib\asyncio\runners.py", line 190, in run
  |     return runner.run(main)
  |            ^^^^^^^^^^^^^^^^
  |   File "C:\Users\GSM078\AppData\Local\Programs\Python\Python311\Lib\asyncio\runners.py", line 118, in run
  |     return self._loop.run_until_complete(task)
  |            ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  |   File "C:\Users\GSM078\AppData\Local\Programs\Python\Python311\Lib\asyncio\base_events.py", line 653, in run_until_complete
  |     return future.result()
  |            ^^^^^^^^^^^^^^^
  |   File "C:\mcp-poc01\mcp-clients\github-official\client-native-http.py", line 16, in main
  |     async with streamablehttp_client(
  |   File "C:\Users\GSM078\AppData\Local\Programs\Python\Python311\Lib\contextlib.py", line 231, in __aexit__
  |     await self.gen.athrow(typ, value, traceback)
  |   File "c:\opt\python-virtual-env\mcphttp\Lib\site-packages\mcp\client\streamable_http.py", line 459, in streamablehttp_client
  |     async with anyio.create_task_group() as tg:
  |   File "c:\opt\python-virtual-env\mcphttp\Lib\site-packages\anyio\_backends\_asyncio.py", line 772, in __aexit__
  |     raise BaseExceptionGroup(
  | ExceptionGroup: unhandled errors in a TaskGroup (1 sub-exception)
  +-+---------------- 1 ----------------
    | Traceback (most recent call last):
    |   File "c:\opt\python-virtual-env\mcphttp\Lib\site-packages\mcp\client\streamable_http.py", line 388, in handle_request_async
    |     await self._handle_post_request(ctx)
    |   File "c:\opt\python-virtual-env\mcphttp\Lib\site-packages\mcp\client\streamable_http.py", line 266, in _handle_post_request
    |     response.raise_for_status()
    |   File "c:\opt\python-virtual-env\mcphttp\Lib\site-packages\httpx\_models.py", line 829, in raise_for_status
    |     raise HTTPStatusError(message, request=request, response=self)
    | httpx.HTTPStatusError: Client error '400 Bad Request' for url 'https://api.githubcopilot.com/mcp/'
For more information check: https://developer.mozilla.org/en-US/docs/Web/HTTP/Status/400
    +------------------------------------

Thank you

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions