qjtrader-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,9 @@
1
+ .venv/
2
+ __pycache__/
3
+ *.py[cod]
4
+ *.egg-info/
5
+ dist/
6
+ build/
7
+ .pytest_cache/
8
+ .mypy_cache/
9
+ .ruff_cache/
@@ -0,0 +1,201 @@
1
+ Apache License
2
+ Version 2.0, January 2004
3
+ http://www.apache.org/licenses/
4
+
5
+ TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
6
+
7
+ 1. Definitions.
8
+
9
+ "License" shall mean the terms and conditions for use, reproduction,
10
+ and distribution as defined by Sections 1 through 9 of this document.
11
+
12
+ "Licensor" shall mean the copyright owner or entity authorized by
13
+ the copyright owner that is granting the License.
14
+
15
+ "Legal Entity" shall mean the union of the acting entity and all
16
+ other entities that control, are controlled by, or are under common
17
+ control with that entity. For the purposes of this definition,
18
+ "control" means (i) the power, direct or indirect, to cause the
19
+ direction or management of such entity, whether by contract or
20
+ otherwise, or (ii) ownership of fifty percent (50%) or more of the
21
+ outstanding shares, or (iii) beneficial ownership of such entity.
22
+
23
+ "You" (or "Your") shall mean an individual or Legal Entity
24
+ exercising permissions granted by this License.
25
+
26
+ "Source" form shall mean the preferred form for making modifications,
27
+ including but not limited to software source code, documentation
28
+ source, and configuration files.
29
+
30
+ "Object" form shall mean any form resulting from mechanical
31
+ transformation or translation of a Source form, including but
32
+ not limited to compiled object code, generated documentation,
33
+ and conversions to other media types.
34
+
35
+ "Work" shall mean the work of authorship, whether in Source or
36
+ Object form, made available under the License, as indicated by a
37
+ copyright notice that is included in or attached to the work
38
+ (an example is provided in the Appendix below).
39
+
40
+ "Derivative Works" shall mean any work, whether in Source or Object
41
+ form, that is based on (or derived from) the Work and for which the
42
+ editorial revisions, annotations, elaborations, or other modifications
43
+ represent, as a whole, an original work of authorship. For the purposes
44
+ of this License, Derivative Works shall not include works that remain
45
+ separable from, or merely link (or bind by name) to the interfaces of,
46
+ the Work and Derivative Works thereof.
47
+
48
+ "Contribution" shall mean any work of authorship, including
49
+ the original version of the Work and any modifications or additions
50
+ to that Work or Derivative Works thereof, that is intentionally
51
+ submitted to Licensor for inclusion in the Work by the copyright owner
52
+ or by an individual or Legal Entity authorized to submit on behalf of
53
+ the copyright owner. For the purposes of this definition, "submitted"
54
+ means any form of electronic, verbal, or written communication sent
55
+ to the Licensor or its representatives, including but not limited to
56
+ communication on electronic mailing lists, source code control systems,
57
+ and issue tracking systems that are managed by, or on behalf of, the
58
+ Licensor for the purpose of discussing and improving the Work, but
59
+ excluding communication that is conspicuously marked or otherwise
60
+ designated in writing by the copyright owner as "Not a Contribution."
61
+
62
+ "Contributor" shall mean Licensor and any individual or Legal Entity
63
+ on behalf of whom a Contribution has been received by Licensor and
64
+ subsequently incorporated within the Work.
65
+
66
+ 2. Grant of Copyright License. Subject to the terms and conditions of
67
+ this License, each Contributor hereby grants to You a perpetual,
68
+ worldwide, non-exclusive, no-charge, royalty-free, irrevocable
69
+ copyright license to reproduce, prepare Derivative Works of,
70
+ publicly display, publicly perform, sublicense, and distribute the
71
+ Work and such Derivative Works in Source or Object form.
72
+
73
+ 3. Grant of Patent License. Subject to the terms and conditions of
74
+ this License, each Contributor hereby grants to You a perpetual,
75
+ worldwide, non-exclusive, no-charge, royalty-free, irrevocable
76
+ (except as stated in this section) patent license to make, have made,
77
+ use, offer to sell, sell, import, and otherwise transfer the Work,
78
+ where such license applies only to those patent claims licensable
79
+ by such Contributor that are necessarily infringed by their
80
+ Contribution(s) alone or by combination of their Contribution(s)
81
+ with the Work to which such Contribution(s) was submitted. If You
82
+ institute patent litigation against any entity (including a
83
+ cross-claim or counterclaim in a lawsuit) alleging that the Work
84
+ or a Contribution incorporated within the Work constitutes direct
85
+ or contributory patent infringement, then any patent licenses
86
+ granted to You under this License for that Work shall terminate
87
+ as of the date such litigation is filed.
88
+
89
+ 4. Redistribution. You may reproduce and distribute copies of the
90
+ Work or Derivative Works thereof in any medium, with or without
91
+ modifications, and in Source or Object form, provided that You
92
+ meet the following conditions:
93
+
94
+ (a) You must give any other recipients of the Work or Derivative
95
+ Works a copy of this License; and
96
+
97
+ (b) You must cause any modified files to carry prominent notices
98
+ stating that You changed the files; and
99
+
100
+ (c) You must retain, in the Source form of any Derivative Works
101
+ that You distribute, all copyright, patent, trademark, and
102
+ attribution notices from the Source form of the Work,
103
+ excluding those notices that do not pertain to any part of
104
+ the Derivative Works; and
105
+
106
+ (d) If the Work includes a "NOTICE" text file as part of its
107
+ distribution, then any Derivative Works that You distribute must
108
+ include a readable copy of the attribution notices contained
109
+ within such NOTICE file, excluding those notices that do not
110
+ pertain to any part of the Derivative Works, in at least one
111
+ of the following places: within a NOTICE text file distributed
112
+ as part of the Derivative Works; within the Source form or
113
+ documentation, if provided along with the Derivative Works; or,
114
+ within a display generated by the Derivative Works, if and
115
+ wherever such third-party notices normally appear. The contents
116
+ of the NOTICE file are for informational purposes only and
117
+ do not modify the License. You may add Your own attribution
118
+ notices within Derivative Works that You distribute, alongside
119
+ or as an addendum to the NOTICE text from the Work, provided
120
+ that such additional attribution notices cannot be construed
121
+ as modifying the License.
122
+
123
+ You may add Your own copyright statement to Your modifications and
124
+ may provide additional or different license terms and conditions
125
+ for use, reproduction, or distribution of Your modifications, or
126
+ for any such Derivative Works as a whole, provided Your use,
127
+ reproduction, and distribution of the Work otherwise complies with
128
+ the conditions stated in this License.
129
+
130
+ 5. Submission of Contributions. Unless You explicitly state otherwise,
131
+ any Contribution intentionally submitted for inclusion in the Work
132
+ by You to the Licensor shall be under the terms and conditions of
133
+ this License, without any additional terms or conditions.
134
+ Notwithstanding the above, nothing herein shall supersede or modify
135
+ the terms of any separate license agreement you may have executed
136
+ with Licensor regarding such Contributions.
137
+
138
+ 6. Trademarks. This License does not grant permission to use the trade
139
+ names, trademarks, service marks, or product names of the Licensor,
140
+ except as required for reasonable and customary use in describing the
141
+ origin of the Work and reproducing the content of the NOTICE file.
142
+
143
+ 7. Disclaimer of Warranty. Unless required by applicable law or
144
+ agreed to in writing, Licensor provides the Work (and each
145
+ Contributor provides its Contributions) on an "AS IS" BASIS,
146
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
147
+ implied, including, without limitation, any warranties or conditions
148
+ of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
149
+ PARTICULAR PURPOSE. You are solely responsible for determining the
150
+ appropriateness of using or redistributing the Work and assume any
151
+ risks associated with Your exercise of permissions under this License.
152
+
153
+ 8. Limitation of Liability. In no event and under no legal theory,
154
+ whether in tort (including negligence), contract, or otherwise,
155
+ unless required by applicable law (such as deliberate and grossly
156
+ negligent acts) or agreed to in writing, shall any Contributor be
157
+ liable to You for damages, including any direct, indirect, special,
158
+ incidental, or consequential damages of any character arising as a
159
+ result of this License or out of the use or inability to use the
160
+ Work (including but not limited to damages for loss of goodwill,
161
+ work stoppage, computer failure or malfunction, or any and all
162
+ other commercial damages or losses), even if such Contributor
163
+ has been advised of the possibility of such damages.
164
+
165
+ 9. Accepting Warranty or Additional Liability. While redistributing
166
+ the Work or Derivative Works thereof, You may choose to offer,
167
+ and charge a fee for, acceptance of support, warranty, indemnity,
168
+ or other liability obligations and/or rights consistent with this
169
+ License. However, in accepting such obligations, You may act only
170
+ on Your own behalf and on Your sole responsibility, not on behalf
171
+ of any other Contributor, and only if You agree to indemnify,
172
+ defend, and hold each Contributor harmless for any liability
173
+ incurred by, or claims asserted against, such Contributor by reason
174
+ of your accepting any such warranty or additional liability.
175
+
176
+ END OF TERMS AND CONDITIONS
177
+
178
+ APPENDIX: How to apply the Apache License to your work.
179
+
180
+ To apply the Apache License to your work, attach the following
181
+ boilerplate notice, with the fields enclosed by brackets "[]"
182
+ replaced with your own identifying information. (Don't include
183
+ the brackets!) The text should be enclosed in the appropriate
184
+ comment syntax for the file format. We also recommend that a
185
+ file or class name and description of purpose be included on the
186
+ same "printed page" as the copyright notice for easier
187
+ identification within third-party archives.
188
+
189
+ Copyright 2026 QJ Trader
190
+
191
+ Licensed under the Apache License, Version 2.0 (the "License");
192
+ you may not use this file except in compliance with the License.
193
+ You may obtain a copy of the License at
194
+
195
+ http://www.apache.org/licenses/LICENSE-2.0
196
+
197
+ Unless required by applicable law or agreed to in writing, software
198
+ distributed under the License is distributed on an "AS IS" BASIS,
199
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
200
+ See the License for the specific language governing permissions and
201
+ limitations under the License.
@@ -0,0 +1,146 @@
1
+ Metadata-Version: 2.4
2
+ Name: qjtrader-mcp
3
+ Version: 0.1.0
4
+ Summary: Model Context Protocol server for the QJ Trader AI Trading APIs — let an LLM watch Canadian market data and place simulated orders.
5
+ Project-URL: Homepage, https://qjtrader.ai
6
+ Project-URL: Documentation, https://docs.qjtrader.ai/docs/ai
7
+ Project-URL: Console, https://console.qjtrader.ai
8
+ Project-URL: Source, https://github.com/QJTrader/qjtrader-python
9
+ Project-URL: Issues, https://github.com/QJTrader/qjtrader-python/issues
10
+ Author: QJ Trader
11
+ License: Apache-2.0
12
+ License-File: LICENSE
13
+ Keywords: canada,claude,llm,market-data,mcp,model-context-protocol,order-entry,trading
14
+ Classifier: Development Status :: 4 - Beta
15
+ Classifier: Intended Audience :: Developers
16
+ Classifier: Intended Audience :: Financial and Insurance Industry
17
+ Classifier: License :: OSI Approved :: Apache Software License
18
+ Classifier: Programming Language :: Python :: 3
19
+ Classifier: Topic :: Office/Business :: Financial :: Investment
20
+ Requires-Python: >=3.10
21
+ Requires-Dist: mcp>=1.2.0
22
+ Requires-Dist: qjtrader>=0.1.0
23
+ Description-Content-Type: text/markdown
24
+
25
+ # qjtrader-mcp
26
+
27
+ **Model Context Protocol server for the [QJ Trader](https://qjtrader.ai) AI Trading APIs.**
28
+ Point your LLM at your QJ credential and it can watch live Canadian market data and place
29
+ **simulated** orders — no code, no manual API testing.
30
+
31
+ Built on the official [`qjtrader`](https://github.com/QJTrader/qjtrader-python) Python SDK. Talk to
32
+ it from Claude Code, Claude Desktop, or any MCP-capable client:
33
+
34
+ > *"Subscribe to CA:RY and MX:CRAU26, show me the books, then buy 1 CRAU26 at 97 in the sandbox
35
+ > and tell me the fill."*
36
+
37
+ ## Safety model — AI trades simulated by default
38
+
39
+ Order-mutating tools (`place_order`, `cancel_order`, `replace_order`, `cancel_all`) run against a
40
+ **sandbox** credential by default and return simulated fills. A **live** credential is refused
41
+ unless you explicitly opt in. The server never sniffs this off the wire (the protocol doesn't
42
+ expose it) — you declare it:
43
+
44
+ | `QJ_ENV` | Read tools (quotes/depth/status) | Order tools |
45
+ |---|---|---|
46
+ | `sandbox` | ✅ | ✅ simulated |
47
+ | `live` | ✅ | ⛔ unless `QJ_MCP_ALLOW_LIVE=1` |
48
+ | *(unset)* | ✅ | ⛔ (fail-safe: unknown is treated as live) |
49
+
50
+ Every tool result is prefixed with an environment tag (`[SANDBOX]` / `[LIVE — REAL MONEY]` /
51
+ `[ENV UNKNOWN]`), and order quantity is capped client-side by `QJ_MCP_MAX_QTY` (default 25).
52
+
53
+ ## Install
54
+
55
+ Get a free sandbox credential (no approval) at [console.qjtrader.ai](https://console.qjtrader.ai).
56
+
57
+ Once published to PyPI, the zero-install path is:
58
+
59
+ ```bash
60
+ uvx qjtrader-mcp # or: pipx run qjtrader-mcp
61
+ ```
62
+
63
+ Until then (or for local development against the SDK checkout):
64
+
65
+ ```bash
66
+ # from the qjtrader-mcp/ directory, with the qjtrader-python sibling checked out:
67
+ uv sync && uv run qjtrader-mcp # uv resolves qjtrader from ../qjtrader-python
68
+ # — or with pip —
69
+ pip install -e ../qjtrader-python -e .
70
+ qjtrader-mcp
71
+ ```
72
+
73
+ ## Configure your client
74
+
75
+ ### Claude Code
76
+
77
+ ```bash
78
+ claude mcp add qjtrader -- uvx qjtrader-mcp
79
+ # then set the credential + environment for the server:
80
+ claude mcp add qjtrader \
81
+ -e QJ_CLIENT_ID=your-client-id \
82
+ -e QJ_CLIENT_SECRET=your-client-secret \
83
+ -e QJ_ENV=sandbox \
84
+ -- uvx qjtrader-mcp
85
+ ```
86
+
87
+ ### Claude Desktop / generic stdio
88
+
89
+ Add to your MCP config (`claude_desktop_config.json` or equivalent):
90
+
91
+ ```json
92
+ {
93
+ "mcpServers": {
94
+ "qjtrader": {
95
+ "command": "uvx",
96
+ "args": ["qjtrader-mcp"],
97
+ "env": {
98
+ "QJ_CLIENT_ID": "your-client-id",
99
+ "QJ_CLIENT_SECRET": "your-client-secret",
100
+ "QJ_ENV": "sandbox"
101
+ }
102
+ }
103
+ }
104
+ }
105
+ ```
106
+
107
+ The console's "Connect your AI" panel generates these blocks pre-filled, including
108
+ `QJ_ENV=sandbox`.
109
+
110
+ ## Tools
111
+
112
+ | Tool | Kind | Description |
113
+ |---|---|---|
114
+ | `session_info` | read | Environment, whether order actions are allowed, endpoints, authenticated user. **Call first.** |
115
+ | `get_quote` | read | Top-of-book (best bid/ask) for one or more symbols |
116
+ | `get_depth` | read | Level-2 order book for a symbol (venue-tagged on consolidated books) |
117
+ | `watch` | read | Sample the live stream for a bounded window; returns a digest + last messages |
118
+ | `list_orders` | read | Open orders + session state |
119
+ | `place_order` | write | Submit a limit order and wait for a terminal state |
120
+ | `cancel_order` | write | Cancel a working order by `cid` |
121
+ | `replace_order` | write | Amend a working order's qty/price |
122
+ | `cancel_all` | write | Cancel every working order (kill switch) |
123
+ | `explain_symbol` | util | Parse/explain a symbol (prefix + root + venue), offline |
124
+ | `read_events` | read | Order journal — cross-order event history; post-trade analysis & strategy debugging |
125
+ | `get_history` | read | Historical OHLCV bars (1s/1m); sandbox = deterministic synthetic days |
126
+ | `get_stats` | read | Server digest for a symbol: VWAP, spread, volume, realized vol (a digest, not a dump) |
127
+ | `get_chain` | read | Options chain snapshot for an underlying/expiry (latest or historical) |
128
+ | `compare` | read | Rank a digest metric (vwap/volume/realized_vol/spread_mean) across symbols |
129
+
130
+ The `read_*`/`get_*` research tools make the LLM a **quant developer**: analyse the
131
+ market and debug the strategy it wrote, without touching the production order path.
132
+
133
+ ## Configuration reference
134
+
135
+ | Env var | Purpose | Default |
136
+ |---|---|---|
137
+ | `QJ_CLIENT_ID` / `QJ_CLIENT_SECRET` | credential | — (required) |
138
+ | `QJ_ENV` | `sandbox` \| `live` — declares the environment | unset → treated as live |
139
+ | `QJ_MCP_ALLOW_LIVE` | set `1` to authorize live order actions | off |
140
+ | `QJ_MCP_MAX_QTY` | client-side max order quantity | `25` |
141
+ | `QJ_DATA_HOST` / `QJ_ORDERS_HOST` | endpoint overrides | public QJ hosts |
142
+ | `QJ_CA_FILE` | pin a CA/cert (pilot order endpoint) | none |
143
+
144
+ ## License
145
+
146
+ Apache-2.0. See [LICENSE](LICENSE).
@@ -0,0 +1,122 @@
1
+ # qjtrader-mcp
2
+
3
+ **Model Context Protocol server for the [QJ Trader](https://qjtrader.ai) AI Trading APIs.**
4
+ Point your LLM at your QJ credential and it can watch live Canadian market data and place
5
+ **simulated** orders — no code, no manual API testing.
6
+
7
+ Built on the official [`qjtrader`](https://github.com/QJTrader/qjtrader-python) Python SDK. Talk to
8
+ it from Claude Code, Claude Desktop, or any MCP-capable client:
9
+
10
+ > *"Subscribe to CA:RY and MX:CRAU26, show me the books, then buy 1 CRAU26 at 97 in the sandbox
11
+ > and tell me the fill."*
12
+
13
+ ## Safety model — AI trades simulated by default
14
+
15
+ Order-mutating tools (`place_order`, `cancel_order`, `replace_order`, `cancel_all`) run against a
16
+ **sandbox** credential by default and return simulated fills. A **live** credential is refused
17
+ unless you explicitly opt in. The server never sniffs this off the wire (the protocol doesn't
18
+ expose it) — you declare it:
19
+
20
+ | `QJ_ENV` | Read tools (quotes/depth/status) | Order tools |
21
+ |---|---|---|
22
+ | `sandbox` | ✅ | ✅ simulated |
23
+ | `live` | ✅ | ⛔ unless `QJ_MCP_ALLOW_LIVE=1` |
24
+ | *(unset)* | ✅ | ⛔ (fail-safe: unknown is treated as live) |
25
+
26
+ Every tool result is prefixed with an environment tag (`[SANDBOX]` / `[LIVE — REAL MONEY]` /
27
+ `[ENV UNKNOWN]`), and order quantity is capped client-side by `QJ_MCP_MAX_QTY` (default 25).
28
+
29
+ ## Install
30
+
31
+ Get a free sandbox credential (no approval) at [console.qjtrader.ai](https://console.qjtrader.ai).
32
+
33
+ Once published to PyPI, the zero-install path is:
34
+
35
+ ```bash
36
+ uvx qjtrader-mcp # or: pipx run qjtrader-mcp
37
+ ```
38
+
39
+ Until then (or for local development against the SDK checkout):
40
+
41
+ ```bash
42
+ # from the qjtrader-mcp/ directory, with the qjtrader-python sibling checked out:
43
+ uv sync && uv run qjtrader-mcp # uv resolves qjtrader from ../qjtrader-python
44
+ # — or with pip —
45
+ pip install -e ../qjtrader-python -e .
46
+ qjtrader-mcp
47
+ ```
48
+
49
+ ## Configure your client
50
+
51
+ ### Claude Code
52
+
53
+ ```bash
54
+ claude mcp add qjtrader -- uvx qjtrader-mcp
55
+ # then set the credential + environment for the server:
56
+ claude mcp add qjtrader \
57
+ -e QJ_CLIENT_ID=your-client-id \
58
+ -e QJ_CLIENT_SECRET=your-client-secret \
59
+ -e QJ_ENV=sandbox \
60
+ -- uvx qjtrader-mcp
61
+ ```
62
+
63
+ ### Claude Desktop / generic stdio
64
+
65
+ Add to your MCP config (`claude_desktop_config.json` or equivalent):
66
+
67
+ ```json
68
+ {
69
+ "mcpServers": {
70
+ "qjtrader": {
71
+ "command": "uvx",
72
+ "args": ["qjtrader-mcp"],
73
+ "env": {
74
+ "QJ_CLIENT_ID": "your-client-id",
75
+ "QJ_CLIENT_SECRET": "your-client-secret",
76
+ "QJ_ENV": "sandbox"
77
+ }
78
+ }
79
+ }
80
+ }
81
+ ```
82
+
83
+ The console's "Connect your AI" panel generates these blocks pre-filled, including
84
+ `QJ_ENV=sandbox`.
85
+
86
+ ## Tools
87
+
88
+ | Tool | Kind | Description |
89
+ |---|---|---|
90
+ | `session_info` | read | Environment, whether order actions are allowed, endpoints, authenticated user. **Call first.** |
91
+ | `get_quote` | read | Top-of-book (best bid/ask) for one or more symbols |
92
+ | `get_depth` | read | Level-2 order book for a symbol (venue-tagged on consolidated books) |
93
+ | `watch` | read | Sample the live stream for a bounded window; returns a digest + last messages |
94
+ | `list_orders` | read | Open orders + session state |
95
+ | `place_order` | write | Submit a limit order and wait for a terminal state |
96
+ | `cancel_order` | write | Cancel a working order by `cid` |
97
+ | `replace_order` | write | Amend a working order's qty/price |
98
+ | `cancel_all` | write | Cancel every working order (kill switch) |
99
+ | `explain_symbol` | util | Parse/explain a symbol (prefix + root + venue), offline |
100
+ | `read_events` | read | Order journal — cross-order event history; post-trade analysis & strategy debugging |
101
+ | `get_history` | read | Historical OHLCV bars (1s/1m); sandbox = deterministic synthetic days |
102
+ | `get_stats` | read | Server digest for a symbol: VWAP, spread, volume, realized vol (a digest, not a dump) |
103
+ | `get_chain` | read | Options chain snapshot for an underlying/expiry (latest or historical) |
104
+ | `compare` | read | Rank a digest metric (vwap/volume/realized_vol/spread_mean) across symbols |
105
+
106
+ The `read_*`/`get_*` research tools make the LLM a **quant developer**: analyse the
107
+ market and debug the strategy it wrote, without touching the production order path.
108
+
109
+ ## Configuration reference
110
+
111
+ | Env var | Purpose | Default |
112
+ |---|---|---|
113
+ | `QJ_CLIENT_ID` / `QJ_CLIENT_SECRET` | credential | — (required) |
114
+ | `QJ_ENV` | `sandbox` \| `live` — declares the environment | unset → treated as live |
115
+ | `QJ_MCP_ALLOW_LIVE` | set `1` to authorize live order actions | off |
116
+ | `QJ_MCP_MAX_QTY` | client-side max order quantity | `25` |
117
+ | `QJ_DATA_HOST` / `QJ_ORDERS_HOST` | endpoint overrides | public QJ hosts |
118
+ | `QJ_CA_FILE` | pin a CA/cert (pilot order endpoint) | none |
119
+
120
+ ## License
121
+
122
+ Apache-2.0. See [LICENSE](LICENSE).
@@ -0,0 +1,50 @@
1
+ [build-system]
2
+ requires = ["hatchling"]
3
+ build-backend = "hatchling.build"
4
+
5
+ [project]
6
+ name = "qjtrader-mcp"
7
+ dynamic = ["version"]
8
+ description = "Model Context Protocol server for the QJ Trader AI Trading APIs — let an LLM watch Canadian market data and place simulated orders."
9
+ readme = "README.md"
10
+ requires-python = ">=3.10"
11
+ license = { text = "Apache-2.0" }
12
+ authors = [{ name = "QJ Trader" }]
13
+ keywords = ["mcp", "model-context-protocol", "trading", "market-data", "order-entry", "llm", "claude", "canada"]
14
+ classifiers = [
15
+ "Development Status :: 4 - Beta",
16
+ "Intended Audience :: Developers",
17
+ "Intended Audience :: Financial and Insurance Industry",
18
+ "License :: OSI Approved :: Apache Software License",
19
+ "Programming Language :: Python :: 3",
20
+ "Topic :: Office/Business :: Financial :: Investment",
21
+ ]
22
+ dependencies = [
23
+ "qjtrader>=0.1.0",
24
+ "mcp>=1.2.0",
25
+ ]
26
+
27
+ [project.urls]
28
+ Homepage = "https://qjtrader.ai"
29
+ Documentation = "https://docs.qjtrader.ai/docs/ai"
30
+ Console = "https://console.qjtrader.ai"
31
+ Source = "https://github.com/QJTrader/qjtrader-python"
32
+ Issues = "https://github.com/QJTrader/qjtrader-python/issues"
33
+
34
+ [project.scripts]
35
+ qjtrader-mcp = "qjtrader_mcp:main"
36
+
37
+ [tool.hatch.version]
38
+ path = "src/qjtrader_mcp/_version.py"
39
+
40
+ [tool.hatch.build.targets.wheel]
41
+ packages = ["src/qjtrader_mcp"]
42
+
43
+ # Local development: resolve `qjtrader` from the sibling SDK checkout until it is
44
+ # published to PyPI. `uv sync` / `uv run` honour this; plain pip users should
45
+ # `pip install -e ../qjtrader-python` first (see README).
46
+ [tool.uv.sources]
47
+ qjtrader = { path = "../qjtrader-python", editable = true }
48
+
49
+ [tool.pytest.ini_options]
50
+ testpaths = ["tests"]
@@ -0,0 +1,17 @@
1
+ """qjtrader-mcp — Model Context Protocol server for the QJ Trader AI Trading APIs.
2
+
3
+ Run ``qjtrader-mcp`` (stdio) and point an MCP client at it. See ``server`` for the
4
+ tool surface and ``_guard`` for the sandbox/live safety model.
5
+ """
6
+ from __future__ import annotations
7
+
8
+ from ._version import __version__
9
+
10
+ __all__ = ["__version__", "main"]
11
+
12
+
13
+ def main() -> None:
14
+ # Imported lazily so ``import qjtrader_mcp`` doesn't require the mcp package.
15
+ from .server import main as _main
16
+
17
+ _main()
@@ -0,0 +1,4 @@
1
+ from . import main
2
+
3
+ if __name__ == "__main__":
4
+ main()
@@ -0,0 +1,92 @@
1
+ """Environment detection and the live-trade guard.
2
+
3
+ The QJ wire protocol does **not** tell a connected client whether its credential
4
+ is sandbox or production (the `auth_success` ack carries only `user`/`compid`, and
5
+ `status` carries only orders/sessions). So the MCP server cannot sniff the
6
+ environment off the socket — it is told, explicitly, via ``QJ_ENV``:
7
+
8
+ QJ_ENV=sandbox → simulated data + simulated fills; order tools allowed
9
+ QJ_ENV=live → real venues; order tools refuse unless QJ_MCP_ALLOW_LIVE=1
10
+ (unset) → unknown; order tools refuse (fail safe), read tools work
11
+
12
+ The console's "Connect your AI" panel injects ``QJ_ENV`` when it generates the
13
+ config, so sandbox works out of the box. For hand-written configs the default is
14
+ deliberately safe: an unknown environment is treated like production, because a
15
+ live and a sandbox credential are indistinguishable to us and silently placing a
16
+ real order would be the worst possible failure.
17
+ """
18
+ from __future__ import annotations
19
+
20
+ import os
21
+
22
+ SANDBOX = "sandbox"
23
+ LIVE = "live"
24
+ UNKNOWN = "unknown"
25
+
26
+ _LIVE_ALIASES = {"live", "real", "production", "prod"}
27
+ _SANDBOX_ALIASES = {"sandbox", "sim", "simulated", "demo", "test"}
28
+
29
+
30
+ def environment() -> str:
31
+ """The credential's environment as declared by ``QJ_ENV`` (best-effort)."""
32
+ raw = (os.environ.get("QJ_ENV") or "").strip().lower()
33
+ if raw in _SANDBOX_ALIASES:
34
+ return SANDBOX
35
+ if raw in _LIVE_ALIASES:
36
+ return LIVE
37
+ return UNKNOWN
38
+
39
+
40
+ def allow_live() -> bool:
41
+ """Whether the operator has explicitly authorized live order actions."""
42
+ return os.environ.get("QJ_MCP_ALLOW_LIVE") == "1"
43
+
44
+
45
+ def tag() -> str:
46
+ """A short banner prefixed to every tool result so the model can never lose
47
+ track of which environment it is acting in."""
48
+ env = environment()
49
+ if env == SANDBOX:
50
+ return "[SANDBOX]"
51
+ if env == LIVE:
52
+ return "[LIVE — REAL MONEY]"
53
+ return "[ENV UNKNOWN — set QJ_ENV=sandbox or live]"
54
+
55
+
56
+ def mutations_allowed() -> tuple[bool, str]:
57
+ """Return ``(allowed, reason)`` for order-mutating tools.
58
+
59
+ Allowed when the environment is sandbox, or when live actions are explicitly
60
+ enabled. Refused (safely) for live-or-unknown environments otherwise.
61
+ """
62
+ env = environment()
63
+ if env == SANDBOX:
64
+ return True, "sandbox credential — simulated orders only"
65
+ if allow_live():
66
+ return True, "live order actions explicitly enabled via QJ_MCP_ALLOW_LIVE=1"
67
+ if env == LIVE:
68
+ return (
69
+ False,
70
+ "This credential is LIVE (real money) and live order actions are "
71
+ "disabled by default. To authorize real orders through this MCP "
72
+ "server, set QJ_MCP_ALLOW_LIVE=1 in its environment and restart it.",
73
+ )
74
+ return (
75
+ False,
76
+ "The environment for this credential is unknown, so order actions are "
77
+ "refused as a safety default. If this is a sandbox credential, set "
78
+ "QJ_ENV=sandbox. If it is live and you intend to place real orders, set "
79
+ "QJ_MCP_ALLOW_LIVE=1. Restart the server after changing the environment.",
80
+ )
81
+
82
+
83
+ def max_qty() -> int:
84
+ """Client-side fat-finger cap on order quantity (mirrors the bridge guards).
85
+
86
+ Overridable with ``QJ_MCP_MAX_QTY``; defaults to a small number because an
87
+ AI-authored order should never be large by accident.
88
+ """
89
+ try:
90
+ return max(1, int(os.environ.get("QJ_MCP_MAX_QTY", "25")))
91
+ except ValueError:
92
+ return 25