|
25 | 25 | from typing_extensions import Unpack |
26 | 26 |
|
27 | 27 | from fastmcp.server import FastMCP as FastMCPServer |
28 | | -from fastmcp.server.dependencies import get_http_request |
| 28 | +from fastmcp.server.dependencies import get_http_headers |
29 | 29 | from fastmcp.server.server import FastMCP |
30 | 30 | from fastmcp.utilities.logging import get_logger |
31 | 31 | from fastmcp.utilities.mcp_config import MCPConfig, infer_transport_type_from_url |
|
35 | 35 |
|
36 | 36 | logger = get_logger(__name__) |
37 | 37 |
|
38 | | -# these headers, when forwarded to the remote server, can cause issues |
39 | | -EXCLUDE_HEADERS = { |
40 | | - "content-length", |
41 | | -} |
42 | | - |
43 | 38 |
|
44 | 39 | class SessionKwargs(TypedDict, total=False): |
45 | 40 | """Keyword arguments for the MCP ClientSession constructor.""" |
@@ -138,23 +133,12 @@ def __init__( |
138 | 133 | async def connect_session( |
139 | 134 | self, **session_kwargs: Unpack[SessionKwargs] |
140 | 135 | ) -> AsyncIterator[ClientSession]: |
141 | | - client_kwargs: dict[str, Any] = { |
142 | | - "headers": self.headers, |
143 | | - } |
| 136 | + client_kwargs: dict[str, Any] = {} |
144 | 137 |
|
145 | 138 | # load headers from an active HTTP request, if available. This will only be true |
146 | 139 | # if the client is used in a FastMCP Proxy, in which case the MCP client headers |
147 | 140 | # need to be forwarded to the remote server. |
148 | | - try: |
149 | | - active_request = get_http_request() |
150 | | - for name, value in active_request.headers.items(): |
151 | | - name = name.lower() |
152 | | - if name not in self.headers and name not in { |
153 | | - h.lower() for h in EXCLUDE_HEADERS |
154 | | - }: |
155 | | - client_kwargs["headers"][name] = str(value) |
156 | | - except RuntimeError: |
157 | | - client_kwargs["headers"] = self.headers |
| 141 | + client_kwargs["headers"] = get_http_headers() | self.headers |
158 | 142 |
|
159 | 143 | # sse_read_timeout has a default value set, so we can't pass None without overriding it |
160 | 144 | # instead we simply leave the kwarg out if it's not provided |
@@ -201,25 +185,12 @@ def __init__( |
201 | 185 | async def connect_session( |
202 | 186 | self, **session_kwargs: Unpack[SessionKwargs] |
203 | 187 | ) -> AsyncIterator[ClientSession]: |
204 | | - client_kwargs: dict[str, Any] = { |
205 | | - "headers": self.headers, |
206 | | - } |
| 188 | + client_kwargs: dict[str, Any] = {} |
207 | 189 |
|
208 | 190 | # load headers from an active HTTP request, if available. This will only be true |
209 | 191 | # if the client is used in a FastMCP Proxy, in which case the MCP client headers |
210 | 192 | # need to be forwarded to the remote server. |
211 | | - try: |
212 | | - active_request = get_http_request() |
213 | | - for name, value in active_request.headers.items(): |
214 | | - name = name.lower() |
215 | | - if name not in self.headers and name not in { |
216 | | - h.lower() for h in EXCLUDE_HEADERS |
217 | | - }: |
218 | | - client_kwargs["headers"][name] = str(value) |
219 | | - |
220 | | - except RuntimeError: |
221 | | - client_kwargs["headers"] = self.headers |
222 | | - print(client_kwargs) |
| 193 | + client_kwargs["headers"] = get_http_headers() | self.headers |
223 | 194 |
|
224 | 195 | # sse_read_timeout has a default value set, so we can't pass None without overriding it |
225 | 196 | # instead we simply leave the kwarg out if it's not provided |
|
0 commit comments