Common
Shared utilities used across all ra-mcp packages.
HTTP Client
HTTPClient(user_agent=None, max_retries=_DEFAULT_MAX_RETRIES, backoff_base=_DEFAULT_BACKOFF_BASE, *, http2=False, connect_timeout=10.0, read_timeout=30.0, write_timeout=10.0, pool_timeout=5.0)
Centralized async HTTP client using httpx with comprehensive logging and retry.
Source code in packages/common/src/ra_mcp_common/http_client.py
35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 | |
aclose()
async
Close the underlying httpx client.
Source code in packages/common/src/ra_mcp_common/http_client.py
383 384 385 | |
get_content(url, timeout=30, headers=None)
async
Make a GET request and return raw content. Returns None on 404 or errors.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
url
|
str
|
URL to fetch |
required |
timeout
|
int
|
Request timeout in seconds |
30
|
headers
|
dict[str, str] | None
|
Additional headers |
None
|
Returns:
| Type | Description |
|---|---|
bytes | None
|
Response content as bytes or None |
Source code in packages/common/src/ra_mcp_common/http_client.py
319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 | |
get_json(url, params=None, timeout=30, headers=None)
async
Make a GET request and return JSON response.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
url
|
str
|
Base URL |
required |
params
|
dict[str, str | int] | None
|
Query parameters |
None
|
timeout
|
int
|
Request timeout in seconds (can be overridden by RA_MCP_TIMEOUT env var) |
30
|
headers
|
dict[str, str] | None
|
Additional headers |
None
|
Returns:
| Type | Description |
|---|---|
dict
|
Parsed JSON response |
Raises:
| Type | Description |
|---|---|
TimeoutError
|
On request timeout |
HTTPStatusError
|
On non-success HTTP status code |
ConnectError
|
On network connection error |
JSONDecodeError
|
On invalid JSON response |
Source code in packages/common/src/ra_mcp_common/http_client.py
111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 | |
get_xml(url, params=None, timeout=30, headers=None)
async
Make a GET request and return XML response as bytes.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
url
|
str
|
Base URL |
required |
params
|
dict[str, str | int] | None
|
Query parameters |
None
|
timeout
|
int
|
Request timeout in seconds |
30
|
headers
|
dict[str, str] | None
|
Additional headers |
None
|
Returns:
| Type | Description |
|---|---|
bytes
|
XML response as bytes |
Raises:
| Type | Description |
|---|---|
TimeoutError
|
On request timeout |
HTTPStatusError
|
On non-success HTTP status code |
ConnectError
|
On network connection error |
Source code in packages/common/src/ra_mcp_common/http_client.py
227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 | |
Formatting Utilities
formatting
Shared formatting utilities for MCP tool output.
These functions are used by both search-mcp and browse-mcp formatters to produce plain-text/markdown output suitable for LLM consumption.
format_error_message(error_message, error_suggestions=None)
Format an error message with optional suggestions.
Source code in packages/common/src/ra_mcp_common/formatting.py
15 16 17 18 19 20 21 | |
format_example_browse_command(reference_code, page_numbers, search_term='')
Format an example browse command for display.
Source code in packages/common/src/ra_mcp_common/formatting.py
78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 | |
highlight_keyword_markdown(text_content, search_keyword)
Highlight search keywords using markdown-style bold.
The text markers from the API are already in the correct format. If no markers present, fallback to manual keyword highlighting.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
text_content
|
str
|
Text to search in (may already contain text markers) |
required |
search_keyword
|
str
|
Keyword to highlight |
required |
Returns:
| Type | Description |
|---|---|
str
|
Text with keywords wrapped in bold |
Source code in packages/common/src/ra_mcp_common/formatting.py
95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 | |
iiif_manifest_to_bildvisaren(iiif_manifest_url)
Convert IIIF manifest URL to bildvisaren URL.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
iiif_manifest_url
|
str
|
IIIF manifest URL (e.g., https://lbiiif.riksarkivet.se/arkis!R0002497/manifest) |
required |
Returns:
| Type | Description |
|---|---|
str
|
Bildvisaren URL (e.g., https://sok.riksarkivet.se/bildvisning/R0002497) or empty string if conversion fails |
Source code in packages/common/src/ra_mcp_common/formatting.py
37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 | |
page_id_to_number(page_id)
Extract the numeric page number from a page ID like '_00066' or '_H0000459_00005'.
Splits by underscore and takes the last non-empty part, stripping leading zeros.
Source code in packages/common/src/ra_mcp_common/formatting.py
24 25 26 27 28 29 30 31 32 33 34 | |
trim_page_number(page_number)
Remove leading underscores and zeros from page number, keeping at least one digit.
Source code in packages/common/src/ra_mcp_common/formatting.py
58 59 60 | |
trim_page_numbers(page_numbers)
Remove leading zeros from multiple page numbers.
Source code in packages/common/src/ra_mcp_common/formatting.py
63 64 65 | |
truncate_text(text, max_length, add_ellipsis=True)
Truncate text to maximum length, optionally adding ellipsis.
Source code in packages/common/src/ra_mcp_common/formatting.py
68 69 70 71 72 73 74 75 | |
Telemetry
telemetry
Telemetry convenience wrappers using only opentelemetry-api.
Returns no-op instances when no SDK is configured (zero overhead). All packages get these helpers transitively through ra-mcp-common.
get_meter(name)
Get a meter for the given module name.
Returns a no-op meter when no MeterProvider SDK is configured.
Source code in packages/common/src/ra_mcp_common/telemetry.py
19 20 21 22 23 24 | |
get_tracer(name)
Get a tracer for the given module name.
Returns a no-op tracer when no TracerProvider SDK is configured.
Source code in packages/common/src/ra_mcp_common/telemetry.py
11 12 13 14 15 16 | |