flexorch-mcp 0.1.0__tar.gz

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,32 @@
1
+ name: CI
2
+
3
+ on:
4
+ push:
5
+ branches: [main, develop]
6
+ pull_request:
7
+ branches: [main]
8
+
9
+ jobs:
10
+ test:
11
+ runs-on: ubuntu-latest
12
+ strategy:
13
+ matrix:
14
+ python-version: ["3.10", "3.11", "3.12", "3.13"]
15
+
16
+ steps:
17
+ - uses: actions/checkout@v4
18
+
19
+ - name: Set up Python ${{ matrix.python-version }}
20
+ uses: actions/setup-python@v5
21
+ with:
22
+ python-version: ${{ matrix.python-version }}
23
+
24
+ - name: Install dependencies
25
+ run: |
26
+ pip install -e ".[dev]"
27
+
28
+ - name: Run tests
29
+ run: pytest tests/ -v
30
+
31
+ - name: Type check
32
+ run: mypy src/
@@ -0,0 +1,15 @@
1
+ __pycache__/
2
+ *.py[cod]
3
+ *.egg-info/
4
+ .eggs/
5
+ dist/
6
+ build/
7
+ .venv/
8
+ venv/
9
+ .env
10
+ *.env
11
+ .mypy_cache/
12
+ .pytest_cache/
13
+ .ruff_cache/
14
+ htmlcov/
15
+ .coverage
@@ -0,0 +1,18 @@
1
+ # Changelog
2
+
3
+ All notable changes to `flexorch-mcp` are documented here.
4
+
5
+ ---
6
+
7
+ ## [0.1.0] — 2026-06-24
8
+
9
+ ### Added
10
+ - `process_document` tool — download from URL and process via FlexOrch pipeline (PDF, DOCX, TXT, XLSX, HTML, XML, EML, JPG, PNG, TIFF); 50 MB limit
11
+ - `get_job_status` tool — poll job status until completed or failed; handles data_process and dataset_build job types
12
+ - `get_extraction_result` tool — retrieve all extracted records directly from execution response; up to 100 records inline, `has_more` hint for larger sets
13
+ - `build_dataset` tool — build a structured dataset from a completed execution
14
+ - `search_documents` tool — full-text and semantic search across indexed datasets; supports auto/structured/semantic/hybrid modes (semantic/hybrid requires Pro+)
15
+ - `export_dataset` tool — export datasets as inline text content; supported formats: JSONL, CSV, JSON, MD, XML, RAG (LangChain/LlamaIndex chunks)
16
+ - `flexorch-mcp --check` CLI command for API key validation and connection verification
17
+ - Structured error messages for all FlexOrch API error codes (QUOTA_EXCEEDED, RATE_LIMIT_EXCEEDED, INVALID_API_KEY, etc.)
18
+ - Claude Desktop and Cursor configuration examples
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2026 flexorch
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
@@ -0,0 +1,191 @@
1
+ Metadata-Version: 2.4
2
+ Name: flexorch-mcp
3
+ Version: 0.1.0
4
+ Summary: MCP server for FlexOrch — SDK for machines
5
+ Project-URL: Homepage, https://flexorch.com
6
+ Project-URL: Documentation, https://docs.flexorch.com
7
+ Project-URL: Repository, https://github.com/flexorch/flexorch-mcp
8
+ Project-URL: Changelog, https://github.com/flexorch/flexorch-mcp/blob/main/CHANGELOG.md
9
+ Author-email: Flexorch Technology <founder@flexorch.com>
10
+ License: MIT License
11
+
12
+ Copyright (c) 2026 flexorch
13
+
14
+ Permission is hereby granted, free of charge, to any person obtaining a copy
15
+ of this software and associated documentation files (the "Software"), to deal
16
+ in the Software without restriction, including without limitation the rights
17
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
18
+ copies of the Software, and to permit persons to whom the Software is
19
+ furnished to do so, subject to the following conditions:
20
+
21
+ The above copyright notice and this permission notice shall be included in all
22
+ copies or substantial portions of the Software.
23
+
24
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
25
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
26
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
27
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
28
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
29
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
30
+ SOFTWARE.
31
+ License-File: LICENSE
32
+ Keywords: agents,document-processing,flexorch,llm,mcp,pii
33
+ Classifier: Development Status :: 4 - Beta
34
+ Classifier: Intended Audience :: Developers
35
+ Classifier: License :: OSI Approved :: MIT License
36
+ Classifier: Programming Language :: Python :: 3
37
+ Classifier: Programming Language :: Python :: 3.10
38
+ Classifier: Programming Language :: Python :: 3.11
39
+ Classifier: Programming Language :: Python :: 3.12
40
+ Classifier: Programming Language :: Python :: 3.13
41
+ Classifier: Topic :: Software Development :: Libraries :: Python Modules
42
+ Requires-Python: >=3.10
43
+ Requires-Dist: httpx>=0.27
44
+ Requires-Dist: mcp<2.0,>=1.0
45
+ Provides-Extra: dev
46
+ Requires-Dist: mypy>=1.10; extra == 'dev'
47
+ Requires-Dist: pytest-asyncio>=0.24; extra == 'dev'
48
+ Requires-Dist: pytest>=8.0; extra == 'dev'
49
+ Requires-Dist: respx>=0.21; extra == 'dev'
50
+ Description-Content-Type: text/markdown
51
+
52
+ # flexorch-mcp
53
+
54
+ **MCP server for FlexOrch — SDK for machines.**
55
+
56
+ Connect Claude and other MCP-compatible agents to the [FlexOrch](https://flexorch.com) document intelligence pipeline. Process documents, extract structured data, detect PII, and export LLM-ready datasets — all through natural language tool calls.
57
+
58
+ ---
59
+
60
+ ## What this is
61
+
62
+ `flexorch-mcp` is a thin proxy that exposes the FlexOrch API as MCP tools. All processing happens on FlexOrch's managed infrastructure. A FlexOrch account and API key are required.
63
+
64
+ **For humans writing code:** use [flexorch-sdk](https://github.com/flexorch/flexorch-sdk) (Python) or [flexorch-sdk-js](https://github.com/flexorch/flexorch-sdk-js) (TypeScript).
65
+ **For agents:** use this package.
66
+
67
+ ---
68
+
69
+ ## Tools
70
+
71
+ | Tool | Description |
72
+ |------|-------------|
73
+ | `process_document` | Upload and process a document (PDF, DOCX, TXT, XLSX, HTML, XML, EML, JPG, PNG, TIFF) |
74
+ | `get_job_status` | Poll a processing job until completed or failed |
75
+ | `get_extraction_result` | Get structured extracted fields from a completed job |
76
+ | `build_dataset` | Build a structured dataset from a completed execution |
77
+ | `search_documents` | Semantic search across indexed datasets (Pro+) |
78
+ | `export_dataset` | Export a dataset as JSONL, CSV, JSON, XML, MD, or RAG (LangChain/LlamaIndex chunks) |
79
+
80
+ ---
81
+
82
+ ## Installation
83
+
84
+ ```bash
85
+ pip install flexorch-mcp
86
+ ```
87
+
88
+ Requires Python 3.10+.
89
+
90
+ ---
91
+
92
+ ## Configuration
93
+
94
+ ### Claude Desktop
95
+
96
+ Add to `~/.claude/claude_desktop_config.json`:
97
+
98
+ ```json
99
+ {
100
+ "mcpServers": {
101
+ "flexorch": {
102
+ "command": "flexorch-mcp",
103
+ "env": {
104
+ "FLEXORCH_API_KEY": "dfx_your_key_here"
105
+ }
106
+ }
107
+ }
108
+ }
109
+ ```
110
+
111
+ ### Cursor
112
+
113
+ Add to your Cursor MCP config:
114
+
115
+ ```json
116
+ {
117
+ "flexorch": {
118
+ "command": "flexorch-mcp",
119
+ "env": {
120
+ "FLEXORCH_API_KEY": "dfx_your_key_here"
121
+ }
122
+ }
123
+ }
124
+ ```
125
+
126
+ Get your API key from [app.flexorch.com/settings](https://app.flexorch.com/settings).
127
+
128
+ ---
129
+
130
+ ## Verify connection
131
+
132
+ ```bash
133
+ flexorch-mcp --check
134
+ # → FlexOrch API key: dfx_xxx*** ✓
135
+ # → Connection: OK (api.flexorch.com)
136
+ # → Plan: Starter (1,200 credits/mo)
137
+ # → Tools: 6 registered
138
+ ```
139
+
140
+ ---
141
+
142
+ ## Example agent workflow
143
+
144
+ ```
145
+ User: "Process this invoice and export it as JSONL for fine-tuning."
146
+
147
+ Agent:
148
+ 1. process_document(file_url="https://...") → job_id: 1234
149
+ 2. get_job_status(1234) → completed, execution_id: 567
150
+ 3. get_extraction_result(567) → vendor, total, date, PII masked
151
+ 4. build_dataset(execution_id=567) → job_id: 1235
152
+ 5. get_job_status(1235) → completed, dataset_id: 89
153
+ 6. export_dataset(89, format="jsonl") → inline JSONL content
154
+ ```
155
+
156
+ ---
157
+
158
+ ## Plan limits
159
+
160
+ All FlexOrch plan limits apply to MCP tool calls. Credits are consumed per document processed.
161
+
162
+ | Plan | Credits/mo | Semantic search |
163
+ |------|-----------|----------------|
164
+ | Trial | 1,200 (30 days) | — |
165
+ | Starter | 1,200 | — |
166
+ | Pro | 6,000 | ✓ |
167
+ | Enterprise | Custom | ✓ |
168
+
169
+ ---
170
+
171
+ ## Security
172
+
173
+ - API key is read from the `FLEXORCH_API_KEY` environment variable — never passed as a tool argument
174
+ - No data is stored or cached by this server — stateless proxy
175
+ - PII masking is applied by FlexOrch's pipeline before results are returned
176
+ - All communication with `api.flexorch.com` uses HTTPS
177
+
178
+ ---
179
+
180
+ ## Related
181
+
182
+ - [flexorch-audit](https://github.com/flexorch/flexorch-audit) — Standalone PII detection and document quality scoring (no account required)
183
+ - [flexorch-sdk](https://github.com/flexorch/flexorch-sdk) — Python SDK for developers
184
+ - [flexorch-sdk-js](https://github.com/flexorch/flexorch-sdk-js) — TypeScript SDK for developers
185
+ - [docs.flexorch.com](https://docs.flexorch.com) — Full documentation
186
+
187
+ ---
188
+
189
+ ## License
190
+
191
+ MIT — see [LICENSE](LICENSE).
@@ -0,0 +1,140 @@
1
+ # flexorch-mcp
2
+
3
+ **MCP server for FlexOrch — SDK for machines.**
4
+
5
+ Connect Claude and other MCP-compatible agents to the [FlexOrch](https://flexorch.com) document intelligence pipeline. Process documents, extract structured data, detect PII, and export LLM-ready datasets — all through natural language tool calls.
6
+
7
+ ---
8
+
9
+ ## What this is
10
+
11
+ `flexorch-mcp` is a thin proxy that exposes the FlexOrch API as MCP tools. All processing happens on FlexOrch's managed infrastructure. A FlexOrch account and API key are required.
12
+
13
+ **For humans writing code:** use [flexorch-sdk](https://github.com/flexorch/flexorch-sdk) (Python) or [flexorch-sdk-js](https://github.com/flexorch/flexorch-sdk-js) (TypeScript).
14
+ **For agents:** use this package.
15
+
16
+ ---
17
+
18
+ ## Tools
19
+
20
+ | Tool | Description |
21
+ |------|-------------|
22
+ | `process_document` | Upload and process a document (PDF, DOCX, TXT, XLSX, HTML, XML, EML, JPG, PNG, TIFF) |
23
+ | `get_job_status` | Poll a processing job until completed or failed |
24
+ | `get_extraction_result` | Get structured extracted fields from a completed job |
25
+ | `build_dataset` | Build a structured dataset from a completed execution |
26
+ | `search_documents` | Semantic search across indexed datasets (Pro+) |
27
+ | `export_dataset` | Export a dataset as JSONL, CSV, JSON, XML, MD, or RAG (LangChain/LlamaIndex chunks) |
28
+
29
+ ---
30
+
31
+ ## Installation
32
+
33
+ ```bash
34
+ pip install flexorch-mcp
35
+ ```
36
+
37
+ Requires Python 3.10+.
38
+
39
+ ---
40
+
41
+ ## Configuration
42
+
43
+ ### Claude Desktop
44
+
45
+ Add to `~/.claude/claude_desktop_config.json`:
46
+
47
+ ```json
48
+ {
49
+ "mcpServers": {
50
+ "flexorch": {
51
+ "command": "flexorch-mcp",
52
+ "env": {
53
+ "FLEXORCH_API_KEY": "dfx_your_key_here"
54
+ }
55
+ }
56
+ }
57
+ }
58
+ ```
59
+
60
+ ### Cursor
61
+
62
+ Add to your Cursor MCP config:
63
+
64
+ ```json
65
+ {
66
+ "flexorch": {
67
+ "command": "flexorch-mcp",
68
+ "env": {
69
+ "FLEXORCH_API_KEY": "dfx_your_key_here"
70
+ }
71
+ }
72
+ }
73
+ ```
74
+
75
+ Get your API key from [app.flexorch.com/settings](https://app.flexorch.com/settings).
76
+
77
+ ---
78
+
79
+ ## Verify connection
80
+
81
+ ```bash
82
+ flexorch-mcp --check
83
+ # → FlexOrch API key: dfx_xxx*** ✓
84
+ # → Connection: OK (api.flexorch.com)
85
+ # → Plan: Starter (1,200 credits/mo)
86
+ # → Tools: 6 registered
87
+ ```
88
+
89
+ ---
90
+
91
+ ## Example agent workflow
92
+
93
+ ```
94
+ User: "Process this invoice and export it as JSONL for fine-tuning."
95
+
96
+ Agent:
97
+ 1. process_document(file_url="https://...") → job_id: 1234
98
+ 2. get_job_status(1234) → completed, execution_id: 567
99
+ 3. get_extraction_result(567) → vendor, total, date, PII masked
100
+ 4. build_dataset(execution_id=567) → job_id: 1235
101
+ 5. get_job_status(1235) → completed, dataset_id: 89
102
+ 6. export_dataset(89, format="jsonl") → inline JSONL content
103
+ ```
104
+
105
+ ---
106
+
107
+ ## Plan limits
108
+
109
+ All FlexOrch plan limits apply to MCP tool calls. Credits are consumed per document processed.
110
+
111
+ | Plan | Credits/mo | Semantic search |
112
+ |------|-----------|----------------|
113
+ | Trial | 1,200 (30 days) | — |
114
+ | Starter | 1,200 | — |
115
+ | Pro | 6,000 | ✓ |
116
+ | Enterprise | Custom | ✓ |
117
+
118
+ ---
119
+
120
+ ## Security
121
+
122
+ - API key is read from the `FLEXORCH_API_KEY` environment variable — never passed as a tool argument
123
+ - No data is stored or cached by this server — stateless proxy
124
+ - PII masking is applied by FlexOrch's pipeline before results are returned
125
+ - All communication with `api.flexorch.com` uses HTTPS
126
+
127
+ ---
128
+
129
+ ## Related
130
+
131
+ - [flexorch-audit](https://github.com/flexorch/flexorch-audit) — Standalone PII detection and document quality scoring (no account required)
132
+ - [flexorch-sdk](https://github.com/flexorch/flexorch-sdk) — Python SDK for developers
133
+ - [flexorch-sdk-js](https://github.com/flexorch/flexorch-sdk-js) — TypeScript SDK for developers
134
+ - [docs.flexorch.com](https://docs.flexorch.com) — Full documentation
135
+
136
+ ---
137
+
138
+ ## License
139
+
140
+ MIT — see [LICENSE](LICENSE).
@@ -0,0 +1,56 @@
1
+ [build-system]
2
+ requires = ["hatchling"]
3
+ build-backend = "hatchling.build"
4
+
5
+ [project]
6
+ name = "flexorch-mcp"
7
+ version = "0.1.0"
8
+ description = "MCP server for FlexOrch — SDK for machines"
9
+ readme = "README.md"
10
+ license = { file = "LICENSE" }
11
+ authors = [{ name = "Flexorch Technology", email = "founder@flexorch.com" }]
12
+ keywords = ["mcp", "flexorch", "document-processing", "pii", "llm", "agents"]
13
+ classifiers = [
14
+ "Development Status :: 4 - Beta",
15
+ "Intended Audience :: Developers",
16
+ "License :: OSI Approved :: MIT License",
17
+ "Programming Language :: Python :: 3",
18
+ "Programming Language :: Python :: 3.10",
19
+ "Programming Language :: Python :: 3.11",
20
+ "Programming Language :: Python :: 3.12",
21
+ "Programming Language :: Python :: 3.13",
22
+ "Topic :: Software Development :: Libraries :: Python Modules",
23
+ ]
24
+ requires-python = ">=3.10"
25
+ dependencies = [
26
+ "mcp>=1.0,<2.0",
27
+ "httpx>=0.27",
28
+ ]
29
+
30
+ [project.optional-dependencies]
31
+ dev = [
32
+ "pytest>=8.0",
33
+ "pytest-asyncio>=0.24",
34
+ "respx>=0.21",
35
+ "mypy>=1.10",
36
+ ]
37
+
38
+ [project.scripts]
39
+ flexorch-mcp = "flexorch_mcp.server:main"
40
+
41
+ [project.urls]
42
+ Homepage = "https://flexorch.com"
43
+ Documentation = "https://docs.flexorch.com"
44
+ Repository = "https://github.com/flexorch/flexorch-mcp"
45
+ Changelog = "https://github.com/flexorch/flexorch-mcp/blob/main/CHANGELOG.md"
46
+
47
+ [tool.hatch.build.targets.wheel]
48
+ packages = ["src/flexorch_mcp"]
49
+
50
+ [tool.pytest.ini_options]
51
+ testpaths = ["tests"]
52
+ asyncio_mode = "auto"
53
+
54
+ [tool.mypy]
55
+ python_version = "3.10"
56
+ strict = true
@@ -0,0 +1,2 @@
1
+ __version__ = "0.1.0"
2
+ __all__ = ["__version__"]
@@ -0,0 +1,160 @@
1
+ from __future__ import annotations
2
+
3
+ from urllib.parse import urlparse
4
+ from typing import Any
5
+
6
+ import httpx
7
+
8
+ from .errors import FlexOrchAPIError, DownloadError, FileTooLargeError, map_api_error # noqa: F401
9
+
10
+ _BASE_URL = "https://api.flexorch.com/v1"
11
+ _MAX_DOWNLOAD_BYTES = 50 * 1024 * 1024 # 50 MB
12
+ _CHUNK_SIZE = 65536
13
+
14
+
15
+ def _mask_key(key: str) -> str:
16
+ """Return dfx_xxx*** masked form for logs — never expose the full key."""
17
+ if len(key) <= 7:
18
+ return "***"
19
+ return key[:7] + "***"
20
+
21
+
22
+ class FlexOrchMCPClient:
23
+ """Async HTTP client for FlexOrch API calls within the MCP server.
24
+
25
+ Keeps two httpx.AsyncClient instances:
26
+ - _api: for FlexOrch API requests (carries X-API-KEY header)
27
+ - _downloader: for external file_url downloads (no auth header)
28
+ """
29
+
30
+ def __init__(self, api_key: str, base_url: str = _BASE_URL) -> None:
31
+ self._api_key = api_key
32
+ self._base_url = base_url.rstrip("/")
33
+ self._api = httpx.AsyncClient(
34
+ headers={
35
+ "X-API-KEY": api_key,
36
+ "User-Agent": "flexorch-mcp/0.1.0",
37
+ },
38
+ timeout=httpx.Timeout(30.0, connect=5.0),
39
+ max_redirects=3,
40
+ verify=True,
41
+ )
42
+ self._downloader = httpx.AsyncClient(
43
+ timeout=httpx.Timeout(30.0, connect=5.0),
44
+ max_redirects=3,
45
+ verify=True,
46
+ follow_redirects=True,
47
+ )
48
+
49
+ # ------------------------------------------------------------------
50
+ # Core request helpers
51
+ # ------------------------------------------------------------------
52
+
53
+ def _url(self, path: str) -> str:
54
+ return f"{self._base_url}/{path.lstrip('/')}"
55
+
56
+ def _raise_for_response(self, response: httpx.Response) -> None:
57
+ if not response.is_error:
58
+ return
59
+ retry_after: int | None = None
60
+ error_code = ""
61
+ try:
62
+ body = response.json()
63
+ error_code = body.get("error", {}).get("code", "")
64
+ except Exception:
65
+ pass
66
+ if response.status_code == 429:
67
+ retry_after = int(response.headers.get("Retry-After", 60))
68
+ raise FlexOrchAPIError(
69
+ map_api_error(error_code, retry_after=retry_after),
70
+ status_code=response.status_code,
71
+ error_code=error_code,
72
+ retry_after=retry_after,
73
+ )
74
+
75
+ def _parse(self, response: httpx.Response) -> Any:
76
+ self._raise_for_response(response)
77
+ if response.status_code == 204 or not response.content:
78
+ return None
79
+ return response.json()
80
+
81
+ async def get(self, path: str, **kwargs: Any) -> Any:
82
+ response = await self._api.get(self._url(path), **kwargs)
83
+ return self._parse(response)
84
+
85
+ async def post(self, path: str, **kwargs: Any) -> Any:
86
+ response = await self._api.post(self._url(path), **kwargs)
87
+ return self._parse(response)
88
+
89
+ async def get_raw(self, path: str, **kwargs: Any) -> tuple[bytes, str, str]:
90
+ """Fetch binary/text response without JSON parsing.
91
+
92
+ Returns (content_bytes, content_type, filename).
93
+ Raises FlexOrchAPIError on HTTP errors.
94
+ """
95
+ response = await self._api.get(self._url(path), **kwargs)
96
+ self._raise_for_response(response)
97
+ content_type = response.headers.get("content-type", "application/octet-stream")
98
+ cd = response.headers.get("content-disposition", "")
99
+ filename = ""
100
+ if 'filename="' in cd:
101
+ filename = cd.split('filename="')[1].split('"')[0]
102
+ elif "filename=" in cd:
103
+ filename = cd.split("filename=")[1].split(";")[0].strip()
104
+ return response.content, content_type, filename
105
+
106
+ # ------------------------------------------------------------------
107
+ # File download
108
+ # ------------------------------------------------------------------
109
+
110
+ async def download_file(self, url: str) -> tuple[bytes, str]:
111
+ """Download a file from an external URL and return (bytes, filename).
112
+
113
+ Validates scheme (http/https only) and enforces 50 MB size limit.
114
+ """
115
+ parsed = urlparse(url)
116
+ if parsed.scheme not in ("http", "https"):
117
+ raise ValueError(
118
+ f"Invalid URL scheme '{parsed.scheme}'. "
119
+ "Only http and https URLs are supported."
120
+ )
121
+
122
+ chunks: list[bytes] = []
123
+ total = 0
124
+
125
+ async with self._downloader.stream("GET", url) as response:
126
+ if response.is_error:
127
+ raise DownloadError(
128
+ "Could not download file from provided URL. "
129
+ "Check that the URL is publicly accessible."
130
+ )
131
+ content_length = int(response.headers.get("content-length", 0))
132
+ if content_length > _MAX_DOWNLOAD_BYTES:
133
+ raise FileTooLargeError("File exceeds size limit. Max 50 MB.")
134
+
135
+ async for chunk in response.aiter_bytes(_CHUNK_SIZE):
136
+ total += len(chunk)
137
+ if total > _MAX_DOWNLOAD_BYTES:
138
+ raise FileTooLargeError("File exceeds size limit. Max 50 MB.")
139
+ chunks.append(chunk)
140
+
141
+ content = b"".join(chunks)
142
+ filename = (parsed.path.rstrip("/").split("/")[-1]) or "document"
143
+ return content, filename
144
+
145
+ # ------------------------------------------------------------------
146
+ # Lifecycle
147
+ # ------------------------------------------------------------------
148
+
149
+ async def aclose(self) -> None:
150
+ await self._api.aclose()
151
+ await self._downloader.aclose()
152
+
153
+ async def __aenter__(self) -> FlexOrchMCPClient:
154
+ return self
155
+
156
+ async def __aexit__(self, *_: Any) -> None:
157
+ await self.aclose()
158
+
159
+ def masked_key(self) -> str:
160
+ return _mask_key(self._api_key)
@@ -0,0 +1,51 @@
1
+ from __future__ import annotations
2
+
3
+ FLEXORCH_ERRORS: dict[str, str] = {
4
+ "QUOTA_EXCEEDED": (
5
+ "Credit quota exceeded. Upgrade your FlexOrch plan at app.flexorch.com/settings"
6
+ ),
7
+ "TRIAL_EXPIRED": "Trial period ended. Activate a plan to continue.",
8
+ "RATE_LIMIT_EXCEEDED": "Rate limit reached. Retry in {retry_after} seconds.",
9
+ "UNSUPPORTED_FILE": (
10
+ "Unsupported file type. Supported: PDF, DOCX, TXT, XLSX, HTML, XML, EML, JPG, PNG, TIFF"
11
+ ),
12
+ "PLAN_UPGRADE_REQUIRED": (
13
+ "This feature requires a Pro plan or above. Upgrade at app.flexorch.com/settings"
14
+ ),
15
+ "INVALID_API_KEY": (
16
+ "Invalid API key. Check your FLEXORCH_API_KEY environment variable."
17
+ ),
18
+ "FILE_TOO_LARGE": "File exceeds size limit. Max 50 MB.",
19
+ "NOT_FOUND": "Resource not found. Check the provided ID.",
20
+ }
21
+
22
+ _DEFAULT_ERROR = "An error occurred. Visit app.flexorch.com for support."
23
+
24
+
25
+ def map_api_error(code: str, retry_after: int | None = None) -> str:
26
+ template = FLEXORCH_ERRORS.get(code, _DEFAULT_ERROR)
27
+ if code == "RATE_LIMIT_EXCEEDED" and retry_after is not None:
28
+ return template.format(retry_after=retry_after)
29
+ return template
30
+
31
+
32
+ class FlexOrchAPIError(Exception):
33
+ def __init__(
34
+ self,
35
+ message: str,
36
+ status_code: int = 0,
37
+ error_code: str = "",
38
+ retry_after: int | None = None,
39
+ ) -> None:
40
+ super().__init__(message)
41
+ self.status_code = status_code
42
+ self.error_code = error_code
43
+ self.retry_after = retry_after
44
+
45
+
46
+ class DownloadError(Exception):
47
+ pass
48
+
49
+
50
+ class FileTooLargeError(Exception):
51
+ pass