Skip to content

Commit cf05824

Browse files
cliffhallclaude
andcommitted
style(python): apply ruff format to fetch, git and time
Clears the existing ruff format findings so the new Python gate starts green. Pure reformatting by each server's locked ruff; no behavior change. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Signed-off-by: cliffhall <cliff@futurescale.com>
1 parent e780241 commit cf05824

7 files changed

Lines changed: 264 additions & 154 deletions

File tree

‎src/fetch/src/mcp_server_fetch/server.py‎

Lines changed: 47 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,9 @@ def get_robots_txt_url(url: str) -> str:
6363
return robots_url
6464

6565

66-
async def check_may_autonomously_fetch_url(url: str, user_agent: str, proxy_url: str | None = None) -> None:
66+
async def check_may_autonomously_fetch_url(
67+
url: str, user_agent: str, proxy_url: str | None = None
68+
) -> None:
6769
"""
6870
Check if the URL can be fetched by the user agent according to the robots.txt file.
6971
Raises a McpError if not.
@@ -80,15 +82,19 @@ async def check_may_autonomously_fetch_url(url: str, user_agent: str, proxy_url:
8082
headers={"User-Agent": user_agent},
8183
)
8284
except HTTPError:
83-
raise McpError(ErrorData(
84-
code=INTERNAL_ERROR,
85-
message=f"Failed to fetch robots.txt {robot_txt_url} due to a connection issue",
86-
))
85+
raise McpError(
86+
ErrorData(
87+
code=INTERNAL_ERROR,
88+
message=f"Failed to fetch robots.txt {robot_txt_url} due to a connection issue",
89+
)
90+
)
8791
if response.status_code in (401, 403):
88-
raise McpError(ErrorData(
89-
code=INTERNAL_ERROR,
90-
message=f"When fetching robots.txt ({robot_txt_url}), received status {response.status_code} so assuming that autonomous fetching is not allowed, the user can try manually fetching by using the fetch prompt",
91-
))
92+
raise McpError(
93+
ErrorData(
94+
code=INTERNAL_ERROR,
95+
message=f"When fetching robots.txt ({robot_txt_url}), received status {response.status_code} so assuming that autonomous fetching is not allowed, the user can try manually fetching by using the fetch prompt",
96+
)
97+
)
9298
elif 400 <= response.status_code < 500:
9399
return
94100
robot_txt = response.text
@@ -97,15 +103,17 @@ async def check_may_autonomously_fetch_url(url: str, user_agent: str, proxy_url:
97103
)
98104
robot_parser = Protego.parse(processed_robot_txt)
99105
if not robot_parser.can_fetch(str(url), user_agent):
100-
raise McpError(ErrorData(
101-
code=INTERNAL_ERROR,
102-
message=f"The sites robots.txt ({robot_txt_url}), specifies that autonomous fetching of this page is not allowed, "
103-
f"<useragent>{user_agent}</useragent>\n"
104-
f"<url>{url}</url>"
105-
f"<robots>\n{robot_txt}\n</robots>\n"
106-
f"The assistant must let the user know that it failed to view the page. The assistant may provide further guidance based on the above information.\n"
107-
f"The assistant can tell the user that they can try manually fetching the page by using the fetch prompt within their UI.",
108-
))
106+
raise McpError(
107+
ErrorData(
108+
code=INTERNAL_ERROR,
109+
message=f"The sites robots.txt ({robot_txt_url}), specifies that autonomous fetching of this page is not allowed, "
110+
f"<useragent>{user_agent}</useragent>\n"
111+
f"<url>{url}</url>"
112+
f"<robots>\n{robot_txt}\n</robots>\n"
113+
f"The assistant must let the user know that it failed to view the page. The assistant may provide further guidance based on the above information.\n"
114+
f"The assistant can tell the user that they can try manually fetching the page by using the fetch prompt within their UI.",
115+
)
116+
)
109117

110118

111119
async def fetch_url(
@@ -125,12 +133,16 @@ async def fetch_url(
125133
timeout=30,
126134
)
127135
except HTTPError as e:
128-
raise McpError(ErrorData(code=INTERNAL_ERROR, message=f"Failed to fetch {url}: {e!r}"))
136+
raise McpError(
137+
ErrorData(code=INTERNAL_ERROR, message=f"Failed to fetch {url}: {e!r}")
138+
)
129139
if response.status_code >= 400:
130-
raise McpError(ErrorData(
131-
code=INTERNAL_ERROR,
132-
message=f"Failed to fetch {url} - status code {response.status_code}",
133-
))
140+
raise McpError(
141+
ErrorData(
142+
code=INTERNAL_ERROR,
143+
message=f"Failed to fetch {url} - status code {response.status_code}",
144+
)
145+
)
134146

135147
page_raw = response.text
136148

@@ -232,7 +244,9 @@ async def call_tool(name, arguments: dict) -> list[TextContent]:
232244
raise McpError(ErrorData(code=INVALID_PARAMS, message="URL is required"))
233245

234246
if not ignore_robots_txt:
235-
await check_may_autonomously_fetch_url(url, user_agent_autonomous, proxy_url)
247+
await check_may_autonomously_fetch_url(
248+
url, user_agent_autonomous, proxy_url
249+
)
236250

237251
content, prefix = await fetch_url(
238252
url, user_agent_autonomous, force_raw=args.raw, proxy_url=proxy_url
@@ -241,13 +255,17 @@ async def call_tool(name, arguments: dict) -> list[TextContent]:
241255
if args.start_index >= original_length:
242256
content = "<error>No more content available.</error>"
243257
else:
244-
truncated_content = content[args.start_index : args.start_index + args.max_length]
258+
truncated_content = content[
259+
args.start_index : args.start_index + args.max_length
260+
]
245261
if not truncated_content:
246262
content = "<error>No more content available.</error>"
247263
else:
248264
content = truncated_content
249265
actual_content_length = len(truncated_content)
250-
remaining_content = original_length - (args.start_index + actual_content_length)
266+
remaining_content = original_length - (
267+
args.start_index + actual_content_length
268+
)
251269
# Only add the prompt to continue fetching if there is still remaining content
252270
if actual_content_length == args.max_length and remaining_content > 0:
253271
next_start = args.start_index + actual_content_length
@@ -262,7 +280,9 @@ async def get_prompt(name: str, arguments: dict | None) -> GetPromptResult:
262280
url = arguments["url"]
263281

264282
try:
265-
content, prefix = await fetch_url(url, user_agent_manual, proxy_url=proxy_url)
283+
content, prefix = await fetch_url(
284+
url, user_agent_manual, proxy_url=proxy_url
285+
)
266286
# TODO: after SDK bug is addressed, don't catch the exception
267287
except McpError as e:
268288
return GetPromptResult(

‎src/fetch/tests/test_server.py‎

Lines changed: 47 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -100,13 +100,14 @@ async def test_allows_when_robots_txt_404(self):
100100
with patch("httpx.AsyncClient") as mock_client_class:
101101
mock_client = AsyncMock()
102102
mock_client.get = AsyncMock(return_value=mock_response)
103-
mock_client_class.return_value.__aenter__ = AsyncMock(return_value=mock_client)
103+
mock_client_class.return_value.__aenter__ = AsyncMock(
104+
return_value=mock_client
105+
)
104106
mock_client_class.return_value.__aexit__ = AsyncMock(return_value=None)
105107

106108
# Should not raise
107109
await check_may_autonomously_fetch_url(
108-
"https://example.com/page",
109-
DEFAULT_USER_AGENT_AUTONOMOUS
110+
"https://example.com/page", DEFAULT_USER_AGENT_AUTONOMOUS
110111
)
111112

112113
@pytest.mark.asyncio
@@ -118,13 +119,14 @@ async def test_blocks_when_robots_txt_401(self):
118119
with patch("httpx.AsyncClient") as mock_client_class:
119120
mock_client = AsyncMock()
120121
mock_client.get = AsyncMock(return_value=mock_response)
121-
mock_client_class.return_value.__aenter__ = AsyncMock(return_value=mock_client)
122+
mock_client_class.return_value.__aenter__ = AsyncMock(
123+
return_value=mock_client
124+
)
122125
mock_client_class.return_value.__aexit__ = AsyncMock(return_value=None)
123126

124127
with pytest.raises(McpError):
125128
await check_may_autonomously_fetch_url(
126-
"https://example.com/page",
127-
DEFAULT_USER_AGENT_AUTONOMOUS
129+
"https://example.com/page", DEFAULT_USER_AGENT_AUTONOMOUS
128130
)
129131

130132
@pytest.mark.asyncio
@@ -136,13 +138,14 @@ async def test_blocks_when_robots_txt_403(self):
136138
with patch("httpx.AsyncClient") as mock_client_class:
137139
mock_client = AsyncMock()
138140
mock_client.get = AsyncMock(return_value=mock_response)
139-
mock_client_class.return_value.__aenter__ = AsyncMock(return_value=mock_client)
141+
mock_client_class.return_value.__aenter__ = AsyncMock(
142+
return_value=mock_client
143+
)
140144
mock_client_class.return_value.__aexit__ = AsyncMock(return_value=None)
141145

142146
with pytest.raises(McpError):
143147
await check_may_autonomously_fetch_url(
144-
"https://example.com/page",
145-
DEFAULT_USER_AGENT_AUTONOMOUS
148+
"https://example.com/page", DEFAULT_USER_AGENT_AUTONOMOUS
146149
)
147150

148151
@pytest.mark.asyncio
@@ -155,13 +158,14 @@ async def test_allows_when_robots_txt_allows_all(self):
155158
with patch("httpx.AsyncClient") as mock_client_class:
156159
mock_client = AsyncMock()
157160
mock_client.get = AsyncMock(return_value=mock_response)
158-
mock_client_class.return_value.__aenter__ = AsyncMock(return_value=mock_client)
161+
mock_client_class.return_value.__aenter__ = AsyncMock(
162+
return_value=mock_client
163+
)
159164
mock_client_class.return_value.__aexit__ = AsyncMock(return_value=None)
160165

161166
# Should not raise
162167
await check_may_autonomously_fetch_url(
163-
"https://example.com/page",
164-
DEFAULT_USER_AGENT_AUTONOMOUS
168+
"https://example.com/page", DEFAULT_USER_AGENT_AUTONOMOUS
165169
)
166170

167171
@pytest.mark.asyncio
@@ -174,13 +178,14 @@ async def test_blocks_when_robots_txt_disallows_all(self):
174178
with patch("httpx.AsyncClient") as mock_client_class:
175179
mock_client = AsyncMock()
176180
mock_client.get = AsyncMock(return_value=mock_response)
177-
mock_client_class.return_value.__aenter__ = AsyncMock(return_value=mock_client)
181+
mock_client_class.return_value.__aenter__ = AsyncMock(
182+
return_value=mock_client
183+
)
178184
mock_client_class.return_value.__aexit__ = AsyncMock(return_value=None)
179185

180186
with pytest.raises(McpError):
181187
await check_may_autonomously_fetch_url(
182-
"https://example.com/page",
183-
DEFAULT_USER_AGENT_AUTONOMOUS
188+
"https://example.com/page", DEFAULT_USER_AGENT_AUTONOMOUS
184189
)
185190

186191

@@ -207,12 +212,13 @@ async def test_fetch_html_page(self):
207212
with patch("httpx.AsyncClient") as mock_client_class:
208213
mock_client = AsyncMock()
209214
mock_client.get = AsyncMock(return_value=mock_response)
210-
mock_client_class.return_value.__aenter__ = AsyncMock(return_value=mock_client)
215+
mock_client_class.return_value.__aenter__ = AsyncMock(
216+
return_value=mock_client
217+
)
211218
mock_client_class.return_value.__aexit__ = AsyncMock(return_value=None)
212219

213220
content, prefix = await fetch_url(
214-
"https://example.com/page",
215-
DEFAULT_USER_AGENT_AUTONOMOUS
221+
"https://example.com/page", DEFAULT_USER_AGENT_AUTONOMOUS
216222
)
217223

218224
# HTML is processed, so we check it returns something
@@ -231,13 +237,15 @@ async def test_fetch_html_page_raw(self):
231237
with patch("httpx.AsyncClient") as mock_client_class:
232238
mock_client = AsyncMock()
233239
mock_client.get = AsyncMock(return_value=mock_response)
234-
mock_client_class.return_value.__aenter__ = AsyncMock(return_value=mock_client)
240+
mock_client_class.return_value.__aenter__ = AsyncMock(
241+
return_value=mock_client
242+
)
235243
mock_client_class.return_value.__aexit__ = AsyncMock(return_value=None)
236244

237245
content, prefix = await fetch_url(
238246
"https://example.com/page",
239247
DEFAULT_USER_AGENT_AUTONOMOUS,
240-
force_raw=True
248+
force_raw=True,
241249
)
242250

243251
assert content == html_content
@@ -255,12 +263,13 @@ async def test_fetch_json_returns_raw(self):
255263
with patch("httpx.AsyncClient") as mock_client_class:
256264
mock_client = AsyncMock()
257265
mock_client.get = AsyncMock(return_value=mock_response)
258-
mock_client_class.return_value.__aenter__ = AsyncMock(return_value=mock_client)
266+
mock_client_class.return_value.__aenter__ = AsyncMock(
267+
return_value=mock_client
268+
)
259269
mock_client_class.return_value.__aexit__ = AsyncMock(return_value=None)
260270

261271
content, prefix = await fetch_url(
262-
"https://api.example.com/data",
263-
DEFAULT_USER_AGENT_AUTONOMOUS
272+
"https://api.example.com/data", DEFAULT_USER_AGENT_AUTONOMOUS
264273
)
265274

266275
assert content == json_content
@@ -275,13 +284,14 @@ async def test_fetch_404_raises_error(self):
275284
with patch("httpx.AsyncClient") as mock_client_class:
276285
mock_client = AsyncMock()
277286
mock_client.get = AsyncMock(return_value=mock_response)
278-
mock_client_class.return_value.__aenter__ = AsyncMock(return_value=mock_client)
287+
mock_client_class.return_value.__aenter__ = AsyncMock(
288+
return_value=mock_client
289+
)
279290
mock_client_class.return_value.__aexit__ = AsyncMock(return_value=None)
280291

281292
with pytest.raises(McpError):
282293
await fetch_url(
283-
"https://example.com/notfound",
284-
DEFAULT_USER_AGENT_AUTONOMOUS
294+
"https://example.com/notfound", DEFAULT_USER_AGENT_AUTONOMOUS
285295
)
286296

287297
@pytest.mark.asyncio
@@ -293,13 +303,14 @@ async def test_fetch_500_raises_error(self):
293303
with patch("httpx.AsyncClient") as mock_client_class:
294304
mock_client = AsyncMock()
295305
mock_client.get = AsyncMock(return_value=mock_response)
296-
mock_client_class.return_value.__aenter__ = AsyncMock(return_value=mock_client)
306+
mock_client_class.return_value.__aenter__ = AsyncMock(
307+
return_value=mock_client
308+
)
297309
mock_client_class.return_value.__aexit__ = AsyncMock(return_value=None)
298310

299311
with pytest.raises(McpError):
300312
await fetch_url(
301-
"https://example.com/error",
302-
DEFAULT_USER_AGENT_AUTONOMOUS
313+
"https://example.com/error", DEFAULT_USER_AGENT_AUTONOMOUS
303314
)
304315

305316
@pytest.mark.asyncio
@@ -313,14 +324,18 @@ async def test_fetch_with_proxy(self):
313324
with patch("httpx.AsyncClient") as mock_client_class:
314325
mock_client = AsyncMock()
315326
mock_client.get = AsyncMock(return_value=mock_response)
316-
mock_client_class.return_value.__aenter__ = AsyncMock(return_value=mock_client)
327+
mock_client_class.return_value.__aenter__ = AsyncMock(
328+
return_value=mock_client
329+
)
317330
mock_client_class.return_value.__aexit__ = AsyncMock(return_value=None)
318331

319332
await fetch_url(
320333
"https://example.com/data",
321334
DEFAULT_USER_AGENT_AUTONOMOUS,
322-
proxy_url="http://proxy.example.com:8080"
335+
proxy_url="http://proxy.example.com:8080",
323336
)
324337

325338
# Verify AsyncClient was called with proxy
326-
mock_client_class.assert_called_once_with(proxy="http://proxy.example.com:8080")
339+
mock_client_class.assert_called_once_with(
340+
proxy="http://proxy.example.com:8080"
341+
)

‎src/git/src/mcp_server_git/__init__.py‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
import sys
55
from .server import serve
66

7+
78
@click.command()
89
@click.option("--repository", "-r", type=Path, help="Git repository path")
910
@click.option("-v", "--verbose", count=True)
@@ -20,5 +21,6 @@ def main(repository: Path | None, verbose: bool) -> None:
2021
logging.basicConfig(level=logging_level, stream=sys.stderr)
2122
asyncio.run(serve(repository))
2223

24+
2325
if __name__ == "__main__":
2426
main()

0 commit comments

Comments
 (0)