get_access_token_from_code
Exchange an authorization code for an access token to authenticate with GitHub using OAuth protocol.
Instructions
Exchange authorization code for an access token.
Tool call template
Replace each placeholder with a value matching the input schema. Only required parameters are included; add optional parameters as needed.
{
"id": 1,
"jsonrpc": "2.0",
"method": "tools/call",
"params": {
"arguments": {
"code": "<code>"
},
"name": "get_access_token_from_code"
}
}Input Schema
| Name | Required | Description | Default |
|---|---|---|---|
| code | Yes |
Implementation Reference
- src/github_oauth/server.py:56-80 (handler)The main handler function for the 'get_access_token_from_code' tool. It is decorated with @mcp.tool() for registration and implements the OAuth token exchange using HTTP POST to GitHub's access_token endpoint.
@mcp.tool() async def get_access_token_from_code(code: str) -> str: """Exchange authorization code for an access token.""" global access_token url = "https://github.com/login/oauth/access_token" headers = {"Accept": "application/json"} params = { "client_id": CLIENT_ID, "client_secret": CLIENT_SECRET, "code": code, "redirect_uri": REDIRECT_URI, } async with httpx.AsyncClient() as client: try: response = await client.post(url, headers=headers, params=params, timeout=30.0) response.raise_for_status() data = response.json() access_token = data.get("access_token", "") if access_token: return "Authorization successful! Access token obtained." return "Failed to obtain access token." except Exception as e: print(f"Error fetching access token: {e}") return "Failed to fetch access token."