44from requests import Response
55
66from databricks .sdk .service .serving import (ExternalFunctionRequestHttpMethod ,
7- ServingEndpointsAPI )
8-
7+ ServingEndpointsAPI ,
8+ HttpRequestResponse )
99
1010class ServingEndpointsExt (ServingEndpointsAPI ):
1111
@@ -88,15 +88,30 @@ def http_request(
8888 """
8989 response = Response ()
9090 response .status_code = 200
91- server_response = super ().http_request (
92- connection_name = conn ,
93- method = method ,
94- path = path ,
95- headers = js .dumps (headers ) if headers is not None else None ,
96- json = js .dumps (json ) if json is not None else None ,
97- params = js .dumps (params ) if params is not None else None ,
91+
92+ # We currently don't call super.http_request because we need to pass in response_headers
93+ # This is a temporary fix to get the headers we need for the MCP session id
94+ # TODO: Remove this once we have a better way to get back the response headers
95+ headers_to_capture = ["mcp-session-id" ]
96+ res = self ._api .do (
97+ "POST" ,
98+ "/api/2.0/external-function" ,
99+ body = {
100+ "connection_name" : conn ,
101+ "method" : method .value ,
102+ "path" : path ,
103+ "headers" : js .dumps (headers ) if headers is not None else None ,
104+ "json" : js .dumps (json ) if json is not None else None ,
105+ "params" : js .dumps (params ) if params is not None else None ,
106+ },
107+ headers = {"Accept" : "text/plain" , "Content-Type" : "application/json" },
108+ raw = True ,
109+ response_headers = headers_to_capture
98110 )
99111
112+ # Create HttpRequestResponse from the raw response
113+ server_response = HttpRequestResponse .from_dict (res )
114+
100115 # Read the content from the HttpRequestResponse object
101116 if hasattr (server_response , "contents" ) and hasattr (server_response .contents , "read" ):
102117 raw_content = server_response .contents .read () # Read the bytes
@@ -109,4 +124,9 @@ def http_request(
109124 else :
110125 raise ValueError ("Contents must be bytes." )
111126
127+ # Copy headers from raw response to Response
128+ for header_name in headers_to_capture :
129+ if header_name in res :
130+ response .headers [header_name ] = res [header_name ]
131+
112132 return response
0 commit comments