appport-sdk 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.
- appport_sdk-0.1.0/LICENSE +21 -0
- appport_sdk-0.1.0/MANIFEST.in +4 -0
- appport_sdk-0.1.0/PKG-INFO +196 -0
- appport_sdk-0.1.0/README.md +172 -0
- appport_sdk-0.1.0/examples/demo_tools.py +230 -0
- appport_sdk-0.1.0/pyproject.toml +63 -0
- appport_sdk-0.1.0/setup.cfg +4 -0
- appport_sdk-0.1.0/src/appport_sdk/__init__.py +98 -0
- appport_sdk-0.1.0/src/appport_sdk/cli.py +215 -0
- appport_sdk-0.1.0/src/appport_sdk/decorators.py +336 -0
- appport_sdk-0.1.0/src/appport_sdk/definition.py +310 -0
- appport_sdk-0.1.0/src/appport_sdk/errors.py +71 -0
- appport_sdk-0.1.0/src/appport_sdk/mcp_server.py +244 -0
- appport_sdk-0.1.0/src/appport_sdk/runner.py +158 -0
- appport_sdk-0.1.0/src/appport_sdk.egg-info/PKG-INFO +196 -0
- appport_sdk-0.1.0/src/appport_sdk.egg-info/SOURCES.txt +24 -0
- appport_sdk-0.1.0/src/appport_sdk.egg-info/dependency_links.txt +1 -0
- appport_sdk-0.1.0/src/appport_sdk.egg-info/entry_points.txt +3 -0
- appport_sdk-0.1.0/src/appport_sdk.egg-info/requires.txt +7 -0
- appport_sdk-0.1.0/src/appport_sdk.egg-info/top_level.txt +1 -0
- appport_sdk-0.1.0/tests/test_cli.py +245 -0
- appport_sdk-0.1.0/tests/test_decorators.py +245 -0
- appport_sdk-0.1.0/tests/test_definition.py +218 -0
- appport_sdk-0.1.0/tests/test_mcp.py +199 -0
- appport_sdk-0.1.0/tests/test_runner.py +123 -0
- appport_sdk-0.1.0/tests/test_smoke.py +10 -0
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 AppPort contributors
|
|
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,196 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: appport-sdk
|
|
3
|
+
Version: 0.1.0
|
|
4
|
+
Summary: Define once, expose twice: turn one Python tool definition into an MCP server and a CLI.
|
|
5
|
+
Author: AppPort contributors
|
|
6
|
+
License-Expression: MIT
|
|
7
|
+
Project-URL: Homepage, https://github.com/RookieApe-tao/appport-sdk
|
|
8
|
+
Project-URL: Companion, https://github.com/RookieApe-tao/appport-bridge
|
|
9
|
+
Keywords: mcp,cli,tools,llm,json-schema
|
|
10
|
+
Classifier: Programming Language :: Python :: 3
|
|
11
|
+
Classifier: Programming Language :: Python :: 3.11
|
|
12
|
+
Classifier: Programming Language :: Python :: 3.12
|
|
13
|
+
Classifier: Operating System :: OS Independent
|
|
14
|
+
Requires-Python: >=3.11
|
|
15
|
+
Description-Content-Type: text/markdown
|
|
16
|
+
License-File: LICENSE
|
|
17
|
+
Requires-Dist: mcp<3,>=2
|
|
18
|
+
Requires-Dist: anyio>=4.9
|
|
19
|
+
Requires-Dist: typer>=0.12
|
|
20
|
+
Provides-Extra: test
|
|
21
|
+
Requires-Dist: pytest>=8; extra == "test"
|
|
22
|
+
Requires-Dist: jsonschema>=4; extra == "test"
|
|
23
|
+
Dynamic: license-file
|
|
24
|
+
|
|
25
|
+
# appport-sdk
|
|
26
|
+
|
|
27
|
+
[](https://github.com/RookieApe-tao/appport-sdk/actions/workflows/ci.yml)
|
|
28
|
+
[](https://github.com/RookieApe-tao/appport-sdk)
|
|
29
|
+
|
|
30
|
+
**Define once, expose twice.** Write a Python function, get an MCP server *and* a
|
|
31
|
+
command-line tool from the same definition — no duplicated schemas, no drifting
|
|
32
|
+
help text.
|
|
33
|
+
|
|
34
|
+
```text
|
|
35
|
+
┌──────────────────────────┐
|
|
36
|
+
Python function → │ @tool → ToolDefinition │ → MCP server (stdio, tools/list + tools/call)
|
|
37
|
+
(one definition) └──────────────────────────┘ → CLI (one subcommand per tool, JSON out)
|
|
38
|
+
```
|
|
39
|
+
|
|
40
|
+
Both exits are generated from the **same** `ToolDefinition`:
|
|
41
|
+
`to_json_schema()` feeds the MCP `inputSchema`, `to_cli_args()` feeds the Typer
|
|
42
|
+
options. Every call — from either exit — returns the shared envelope, so an LLM
|
|
43
|
+
and a shell script see identical results.
|
|
44
|
+
|
|
45
|
+
## Quick start
|
|
46
|
+
|
|
47
|
+
```powershell
|
|
48
|
+
python -m venv .venv
|
|
49
|
+
.\.venv\Scripts\python.exe -m pip install -e ".[test]"
|
|
50
|
+
```
|
|
51
|
+
|
|
52
|
+
Requires **`mcp>=2,<3`** and `typer>=0.12` on Python 3.11+. The MCP exit is built
|
|
53
|
+
on the mcp 2.x low-level `Server` API (`on_list_tools` / `on_call_tool`); the
|
|
54
|
+
older 1.x `mcp.server.fastmcp` API no longer exists in 2.x. `appport-bridge`
|
|
55
|
+
already runs on mcp v2, so both repositories pin the same `mcp>=2,<3` window and
|
|
56
|
+
stay installable side by side.
|
|
57
|
+
|
|
58
|
+
Define a tool (`examples/demo_tools.py` has three):
|
|
59
|
+
|
|
60
|
+
```python
|
|
61
|
+
from appport_sdk import Returns, ToolRegistry, tool
|
|
62
|
+
|
|
63
|
+
@tool(returns=Returns(type="str", description="The echoed text."))
|
|
64
|
+
def echo(text: str, loud: bool = False) -> str:
|
|
65
|
+
"""Echo text back.
|
|
66
|
+
|
|
67
|
+
Args:
|
|
68
|
+
text: Text to echo.
|
|
69
|
+
loud: Upper-case the result.
|
|
70
|
+
"""
|
|
71
|
+
return text.upper() if loud else text
|
|
72
|
+
|
|
73
|
+
TOOLS = ToolRegistry([echo])
|
|
74
|
+
```
|
|
75
|
+
|
|
76
|
+
### Exit 1 — CLI
|
|
77
|
+
|
|
78
|
+
```powershell
|
|
79
|
+
appport-sdk --tools examples.demo_tools:TOOLS echo --text "hi" --loud
|
|
80
|
+
```
|
|
81
|
+
```json
|
|
82
|
+
{
|
|
83
|
+
"ok": true,
|
|
84
|
+
"data": "HI"
|
|
85
|
+
}
|
|
86
|
+
```
|
|
87
|
+
|
|
88
|
+
Each tool becomes a subcommand; parameters become `--kebab-case` options, bools
|
|
89
|
+
become `--flag/--no-flag` pairs, and required params are enforced by Click. A
|
|
90
|
+
call that reaches the tool prints the JSON envelope on stdout and exits `0` (or
|
|
91
|
+
`1` when the tool itself fails). A Click-level usage or type error — a missing
|
|
92
|
+
required option, an unparseable value — is reported by Click as usual and exits
|
|
93
|
+
`2` without JSON. Without `--tools` the CLI reads `APPPORT_TOOLS`.
|
|
94
|
+
|
|
95
|
+
### Exit 2 — MCP
|
|
96
|
+
|
|
97
|
+
```powershell
|
|
98
|
+
appport-sdk-mcp --tools examples.demo_tools:TOOLS # or: python -m appport_sdk.mcp_server
|
|
99
|
+
```
|
|
100
|
+
|
|
101
|
+
Add it to any MCP client (Claude Desktop / Cursor / …):
|
|
102
|
+
|
|
103
|
+
```json
|
|
104
|
+
{
|
|
105
|
+
"mcpServers": {
|
|
106
|
+
"appport-demo": {
|
|
107
|
+
"command": "appport-sdk-mcp",
|
|
108
|
+
"args": ["--tools", "examples.demo_tools:TOOLS"]
|
|
109
|
+
}
|
|
110
|
+
}
|
|
111
|
+
}
|
|
112
|
+
```
|
|
113
|
+
|
|
114
|
+
`tools/list` advertises exactly the shared `ToolDefinition` schema
|
|
115
|
+
(`additionalProperties: false`), and `tools/call` returns the envelope:
|
|
116
|
+
|
|
117
|
+
```json
|
|
118
|
+
{"ok": true, "data": "HI"}
|
|
119
|
+
```
|
|
120
|
+
|
|
121
|
+
### Both exits at once
|
|
122
|
+
|
|
123
|
+
```powershell
|
|
124
|
+
python examples/demo_tools.py
|
|
125
|
+
```
|
|
126
|
+
|
|
127
|
+
This drives the CLI through real subprocesses and then starts the MCP server as a
|
|
128
|
+
real stdio subprocess and calls it with the official `mcp` client — the single
|
|
129
|
+
definition, exercised twice.
|
|
130
|
+
|
|
131
|
+
## The shared ToolDefinition spec (v0.1)
|
|
132
|
+
|
|
133
|
+
```python
|
|
134
|
+
{
|
|
135
|
+
"name": "snake_case_unique",
|
|
136
|
+
"description": "what the LLM reads",
|
|
137
|
+
"params": [{"name": str, "type": "str|int|float|bool",
|
|
138
|
+
"description": str, "required": bool, "default": any}],
|
|
139
|
+
"returns": {"type": "str|json", "description": str},
|
|
140
|
+
"examples": [{"args": {...}, "result_hint": str}], # optional
|
|
141
|
+
}
|
|
142
|
+
```
|
|
143
|
+
|
|
144
|
+
Result envelope — success `{"ok": true, "data": ...}`, failure
|
|
145
|
+
`{"ok": false, "error": {"code": ..., "message": ...}}`. Every call that reaches
|
|
146
|
+
the SDK — from MCP or from the CLI — returns that envelope, and the SDK converts
|
|
147
|
+
missing/invalid arguments and any exception raised by your function into the
|
|
148
|
+
failure form; it never lets a raw traceback escape.
|
|
149
|
+
|
|
150
|
+
One caveat on the CLI: Click parses arguments *before* the SDK sees them, so an
|
|
151
|
+
unknown option or an unparseable value is rejected by Click itself with a usage
|
|
152
|
+
message and exit code `2` — no JSON for those. Failures after parsing
|
|
153
|
+
(validation against the definition, your function raising, a `ToolError`) are
|
|
154
|
+
always JSON with exit `1`. MCP calls always go through the SDK, so they get the
|
|
155
|
+
envelope in every case.
|
|
156
|
+
|
|
157
|
+
Error codes: `MISSING_PARAMETER`, `INVALID_ARGUMENT`, `UNKNOWN_PARAMETER`
|
|
158
|
+
(CLI/local calls), `UNKNOWN_TOOL` (an MCP `tools/call` for a name that is not
|
|
159
|
+
registered), `TOOL_ERROR` (your `ToolError`), `INTERNAL_ERROR`.
|
|
160
|
+
|
|
161
|
+
## API notes
|
|
162
|
+
|
|
163
|
+
| Piece | Where | Notes |
|
|
164
|
+
| --- | --- | --- |
|
|
165
|
+
| `ToolDefinition`, `Param`, `Returns`, `Example` | `appport_sdk.definition` | immutable; `to_json_schema()`, `to_cli_args()`, `to_dict()`, `from_dict()` |
|
|
166
|
+
| `@tool(...)` | `appport_sdk.decorators` | returns the *original* function with `.definition` attached; `ToolRegistry` collects them |
|
|
167
|
+
| `invoke()` / `invoke_async()` | `appport_sdk.runner` | validation, coercion, envelope wrapping |
|
|
168
|
+
| `build_cli()` / `serve_cli()` | `appport_sdk.cli` | Typer app; `build_cli` returns an app you can test |
|
|
169
|
+
| `build_mcp_server()` / `serve_mcp()` | `appport_sdk.mcp_server` | mcp v2 low-level `Server` over stdio |
|
|
170
|
+
|
|
171
|
+
* Type mapping: `str → string`, `int → integer`, `float → number`,
|
|
172
|
+
`bool → boolean`. A parameter with a default is `required: false` and its
|
|
173
|
+
default lands in the JSON Schema.
|
|
174
|
+
* `description` comes from `@tool(description=...)`, else the first paragraph of
|
|
175
|
+
the docstring, else the function name.
|
|
176
|
+
* `Optional[str]`-style annotations unwrap to their base type.
|
|
177
|
+
* `mcp>=2,<3` (v2 low-level `Server` API); `typer>=0.12`. Python 3.11+.
|
|
178
|
+
|
|
179
|
+
## Relation to appport-bridge
|
|
180
|
+
|
|
181
|
+
[AppPort Bridge](https://github.com/RookieApe-tao/appport-bridge) gives an
|
|
182
|
+
existing Windows desktop app an MCP port; **this SDK is the other half — it
|
|
183
|
+
produces `ToolDefinition`s in the exact same v0.1 shape**, so tools built here
|
|
184
|
+
and definitions produced by the bridge are interchangeable.
|
|
185
|
+
|
|
186
|
+
## Development
|
|
187
|
+
|
|
188
|
+
```powershell
|
|
189
|
+
.\.venv\Scripts\python.exe -m pytest -q
|
|
190
|
+
```
|
|
191
|
+
|
|
192
|
+
## License
|
|
193
|
+
|
|
194
|
+
MIT
|
|
195
|
+
|
|
196
|
+
Business model: open core with enterprise features.
|
|
@@ -0,0 +1,172 @@
|
|
|
1
|
+
# appport-sdk
|
|
2
|
+
|
|
3
|
+
[](https://github.com/RookieApe-tao/appport-sdk/actions/workflows/ci.yml)
|
|
4
|
+
[](https://github.com/RookieApe-tao/appport-sdk)
|
|
5
|
+
|
|
6
|
+
**Define once, expose twice.** Write a Python function, get an MCP server *and* a
|
|
7
|
+
command-line tool from the same definition — no duplicated schemas, no drifting
|
|
8
|
+
help text.
|
|
9
|
+
|
|
10
|
+
```text
|
|
11
|
+
┌──────────────────────────┐
|
|
12
|
+
Python function → │ @tool → ToolDefinition │ → MCP server (stdio, tools/list + tools/call)
|
|
13
|
+
(one definition) └──────────────────────────┘ → CLI (one subcommand per tool, JSON out)
|
|
14
|
+
```
|
|
15
|
+
|
|
16
|
+
Both exits are generated from the **same** `ToolDefinition`:
|
|
17
|
+
`to_json_schema()` feeds the MCP `inputSchema`, `to_cli_args()` feeds the Typer
|
|
18
|
+
options. Every call — from either exit — returns the shared envelope, so an LLM
|
|
19
|
+
and a shell script see identical results.
|
|
20
|
+
|
|
21
|
+
## Quick start
|
|
22
|
+
|
|
23
|
+
```powershell
|
|
24
|
+
python -m venv .venv
|
|
25
|
+
.\.venv\Scripts\python.exe -m pip install -e ".[test]"
|
|
26
|
+
```
|
|
27
|
+
|
|
28
|
+
Requires **`mcp>=2,<3`** and `typer>=0.12` on Python 3.11+. The MCP exit is built
|
|
29
|
+
on the mcp 2.x low-level `Server` API (`on_list_tools` / `on_call_tool`); the
|
|
30
|
+
older 1.x `mcp.server.fastmcp` API no longer exists in 2.x. `appport-bridge`
|
|
31
|
+
already runs on mcp v2, so both repositories pin the same `mcp>=2,<3` window and
|
|
32
|
+
stay installable side by side.
|
|
33
|
+
|
|
34
|
+
Define a tool (`examples/demo_tools.py` has three):
|
|
35
|
+
|
|
36
|
+
```python
|
|
37
|
+
from appport_sdk import Returns, ToolRegistry, tool
|
|
38
|
+
|
|
39
|
+
@tool(returns=Returns(type="str", description="The echoed text."))
|
|
40
|
+
def echo(text: str, loud: bool = False) -> str:
|
|
41
|
+
"""Echo text back.
|
|
42
|
+
|
|
43
|
+
Args:
|
|
44
|
+
text: Text to echo.
|
|
45
|
+
loud: Upper-case the result.
|
|
46
|
+
"""
|
|
47
|
+
return text.upper() if loud else text
|
|
48
|
+
|
|
49
|
+
TOOLS = ToolRegistry([echo])
|
|
50
|
+
```
|
|
51
|
+
|
|
52
|
+
### Exit 1 — CLI
|
|
53
|
+
|
|
54
|
+
```powershell
|
|
55
|
+
appport-sdk --tools examples.demo_tools:TOOLS echo --text "hi" --loud
|
|
56
|
+
```
|
|
57
|
+
```json
|
|
58
|
+
{
|
|
59
|
+
"ok": true,
|
|
60
|
+
"data": "HI"
|
|
61
|
+
}
|
|
62
|
+
```
|
|
63
|
+
|
|
64
|
+
Each tool becomes a subcommand; parameters become `--kebab-case` options, bools
|
|
65
|
+
become `--flag/--no-flag` pairs, and required params are enforced by Click. A
|
|
66
|
+
call that reaches the tool prints the JSON envelope on stdout and exits `0` (or
|
|
67
|
+
`1` when the tool itself fails). A Click-level usage or type error — a missing
|
|
68
|
+
required option, an unparseable value — is reported by Click as usual and exits
|
|
69
|
+
`2` without JSON. Without `--tools` the CLI reads `APPPORT_TOOLS`.
|
|
70
|
+
|
|
71
|
+
### Exit 2 — MCP
|
|
72
|
+
|
|
73
|
+
```powershell
|
|
74
|
+
appport-sdk-mcp --tools examples.demo_tools:TOOLS # or: python -m appport_sdk.mcp_server
|
|
75
|
+
```
|
|
76
|
+
|
|
77
|
+
Add it to any MCP client (Claude Desktop / Cursor / …):
|
|
78
|
+
|
|
79
|
+
```json
|
|
80
|
+
{
|
|
81
|
+
"mcpServers": {
|
|
82
|
+
"appport-demo": {
|
|
83
|
+
"command": "appport-sdk-mcp",
|
|
84
|
+
"args": ["--tools", "examples.demo_tools:TOOLS"]
|
|
85
|
+
}
|
|
86
|
+
}
|
|
87
|
+
}
|
|
88
|
+
```
|
|
89
|
+
|
|
90
|
+
`tools/list` advertises exactly the shared `ToolDefinition` schema
|
|
91
|
+
(`additionalProperties: false`), and `tools/call` returns the envelope:
|
|
92
|
+
|
|
93
|
+
```json
|
|
94
|
+
{"ok": true, "data": "HI"}
|
|
95
|
+
```
|
|
96
|
+
|
|
97
|
+
### Both exits at once
|
|
98
|
+
|
|
99
|
+
```powershell
|
|
100
|
+
python examples/demo_tools.py
|
|
101
|
+
```
|
|
102
|
+
|
|
103
|
+
This drives the CLI through real subprocesses and then starts the MCP server as a
|
|
104
|
+
real stdio subprocess and calls it with the official `mcp` client — the single
|
|
105
|
+
definition, exercised twice.
|
|
106
|
+
|
|
107
|
+
## The shared ToolDefinition spec (v0.1)
|
|
108
|
+
|
|
109
|
+
```python
|
|
110
|
+
{
|
|
111
|
+
"name": "snake_case_unique",
|
|
112
|
+
"description": "what the LLM reads",
|
|
113
|
+
"params": [{"name": str, "type": "str|int|float|bool",
|
|
114
|
+
"description": str, "required": bool, "default": any}],
|
|
115
|
+
"returns": {"type": "str|json", "description": str},
|
|
116
|
+
"examples": [{"args": {...}, "result_hint": str}], # optional
|
|
117
|
+
}
|
|
118
|
+
```
|
|
119
|
+
|
|
120
|
+
Result envelope — success `{"ok": true, "data": ...}`, failure
|
|
121
|
+
`{"ok": false, "error": {"code": ..., "message": ...}}`. Every call that reaches
|
|
122
|
+
the SDK — from MCP or from the CLI — returns that envelope, and the SDK converts
|
|
123
|
+
missing/invalid arguments and any exception raised by your function into the
|
|
124
|
+
failure form; it never lets a raw traceback escape.
|
|
125
|
+
|
|
126
|
+
One caveat on the CLI: Click parses arguments *before* the SDK sees them, so an
|
|
127
|
+
unknown option or an unparseable value is rejected by Click itself with a usage
|
|
128
|
+
message and exit code `2` — no JSON for those. Failures after parsing
|
|
129
|
+
(validation against the definition, your function raising, a `ToolError`) are
|
|
130
|
+
always JSON with exit `1`. MCP calls always go through the SDK, so they get the
|
|
131
|
+
envelope in every case.
|
|
132
|
+
|
|
133
|
+
Error codes: `MISSING_PARAMETER`, `INVALID_ARGUMENT`, `UNKNOWN_PARAMETER`
|
|
134
|
+
(CLI/local calls), `UNKNOWN_TOOL` (an MCP `tools/call` for a name that is not
|
|
135
|
+
registered), `TOOL_ERROR` (your `ToolError`), `INTERNAL_ERROR`.
|
|
136
|
+
|
|
137
|
+
## API notes
|
|
138
|
+
|
|
139
|
+
| Piece | Where | Notes |
|
|
140
|
+
| --- | --- | --- |
|
|
141
|
+
| `ToolDefinition`, `Param`, `Returns`, `Example` | `appport_sdk.definition` | immutable; `to_json_schema()`, `to_cli_args()`, `to_dict()`, `from_dict()` |
|
|
142
|
+
| `@tool(...)` | `appport_sdk.decorators` | returns the *original* function with `.definition` attached; `ToolRegistry` collects them |
|
|
143
|
+
| `invoke()` / `invoke_async()` | `appport_sdk.runner` | validation, coercion, envelope wrapping |
|
|
144
|
+
| `build_cli()` / `serve_cli()` | `appport_sdk.cli` | Typer app; `build_cli` returns an app you can test |
|
|
145
|
+
| `build_mcp_server()` / `serve_mcp()` | `appport_sdk.mcp_server` | mcp v2 low-level `Server` over stdio |
|
|
146
|
+
|
|
147
|
+
* Type mapping: `str → string`, `int → integer`, `float → number`,
|
|
148
|
+
`bool → boolean`. A parameter with a default is `required: false` and its
|
|
149
|
+
default lands in the JSON Schema.
|
|
150
|
+
* `description` comes from `@tool(description=...)`, else the first paragraph of
|
|
151
|
+
the docstring, else the function name.
|
|
152
|
+
* `Optional[str]`-style annotations unwrap to their base type.
|
|
153
|
+
* `mcp>=2,<3` (v2 low-level `Server` API); `typer>=0.12`. Python 3.11+.
|
|
154
|
+
|
|
155
|
+
## Relation to appport-bridge
|
|
156
|
+
|
|
157
|
+
[AppPort Bridge](https://github.com/RookieApe-tao/appport-bridge) gives an
|
|
158
|
+
existing Windows desktop app an MCP port; **this SDK is the other half — it
|
|
159
|
+
produces `ToolDefinition`s in the exact same v0.1 shape**, so tools built here
|
|
160
|
+
and definitions produced by the bridge are interchangeable.
|
|
161
|
+
|
|
162
|
+
## Development
|
|
163
|
+
|
|
164
|
+
```powershell
|
|
165
|
+
.\.venv\Scripts\python.exe -m pytest -q
|
|
166
|
+
```
|
|
167
|
+
|
|
168
|
+
## License
|
|
169
|
+
|
|
170
|
+
MIT
|
|
171
|
+
|
|
172
|
+
Business model: open core with enterprise features.
|
|
@@ -0,0 +1,230 @@
|
|
|
1
|
+
"""Three tools defined once, exposed twice (CLI + MCP).
|
|
2
|
+
|
|
3
|
+
Run the whole demonstration (real CLI commands, then a real MCP stdio client):
|
|
4
|
+
|
|
5
|
+
python examples/demo_tools.py
|
|
6
|
+
|
|
7
|
+
Run only one exit:
|
|
8
|
+
|
|
9
|
+
python -m appport_sdk.cli --tools examples.demo_tools:TOOLS echo --text hi
|
|
10
|
+
python -m appport_sdk.mcp_server --tools examples.demo_tools:TOOLS
|
|
11
|
+
"""
|
|
12
|
+
|
|
13
|
+
from __future__ import annotations
|
|
14
|
+
|
|
15
|
+
import datetime as _dt
|
|
16
|
+
from typing import Any
|
|
17
|
+
|
|
18
|
+
from appport_sdk import Returns, ToolRegistry, tool
|
|
19
|
+
|
|
20
|
+
# ---------------------------------------------------------------------------
|
|
21
|
+
# The definitions: this is the *only* place a tool is described.
|
|
22
|
+
# ---------------------------------------------------------------------------
|
|
23
|
+
|
|
24
|
+
|
|
25
|
+
@tool(
|
|
26
|
+
description="Echo the given text back, optionally upper-cased and repeated.",
|
|
27
|
+
returns=Returns(type="str", description="The (possibly transformed) text."),
|
|
28
|
+
examples=[{"args": {"text": "hi"}, "result_hint": "hi"}],
|
|
29
|
+
)
|
|
30
|
+
def echo(text: str, loud: bool = False, times: int = 1) -> str:
|
|
31
|
+
"""Echo text.
|
|
32
|
+
|
|
33
|
+
Args:
|
|
34
|
+
text: Text to echo.
|
|
35
|
+
loud: Upper-case the result.
|
|
36
|
+
times: How many times to repeat the text.
|
|
37
|
+
"""
|
|
38
|
+
result = text.upper() if loud else text
|
|
39
|
+
return result * max(times, 0)
|
|
40
|
+
|
|
41
|
+
|
|
42
|
+
@tool(
|
|
43
|
+
description="Add two numbers and return the sum.",
|
|
44
|
+
returns=Returns(type="json", description="The numeric sum."),
|
|
45
|
+
examples=[{"args": {"a": 1, "b": 2.5}, "result_hint": "3.5"}],
|
|
46
|
+
)
|
|
47
|
+
def add(a: float, b: float) -> float:
|
|
48
|
+
"""Add two numbers."""
|
|
49
|
+
return a + b
|
|
50
|
+
|
|
51
|
+
|
|
52
|
+
@tool(
|
|
53
|
+
description="Return the current local time, optionally in another UTC offset.",
|
|
54
|
+
returns=Returns(type="json", description="ISO-8601 timestamp and the zone used."),
|
|
55
|
+
examples=[{"args": {"zone": "UTC"}, "result_hint": "2026-12-01T00:00:00+00:00"}],
|
|
56
|
+
)
|
|
57
|
+
def now(zone: str = "local", utc_offset_hours: float = 0.0) -> dict[str, Any]:
|
|
58
|
+
"""Return the current time.
|
|
59
|
+
|
|
60
|
+
Args:
|
|
61
|
+
zone: Label for the reported time zone.
|
|
62
|
+
utc_offset_hours: Offset from UTC, in hours.
|
|
63
|
+
"""
|
|
64
|
+
moment = _dt.datetime.now(_dt.timezone.utc) + _dt.timedelta(
|
|
65
|
+
hours=utc_offset_hours
|
|
66
|
+
)
|
|
67
|
+
return {
|
|
68
|
+
"iso": moment.replace(tzinfo=None).isoformat(timespec="seconds"),
|
|
69
|
+
"zone": zone,
|
|
70
|
+
"utc_offset_hours": utc_offset_hours,
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
|
|
74
|
+
#: The single collection both exits consume.
|
|
75
|
+
TOOLS = ToolRegistry([echo, add, now])
|
|
76
|
+
|
|
77
|
+
|
|
78
|
+
def _demo_definitions() -> list[dict[str, Any]]:
|
|
79
|
+
return [definition.to_dict() for definition in TOOLS.definitions()]
|
|
80
|
+
|
|
81
|
+
|
|
82
|
+
# ---------------------------------------------------------------------------
|
|
83
|
+
# Demonstration: the same definitions drive a real CLI and a real MCP server.
|
|
84
|
+
# ---------------------------------------------------------------------------
|
|
85
|
+
|
|
86
|
+
#: CLI invocations exercised by the demo (they are also distinct code paths).
|
|
87
|
+
CLI_COMMANDS = [
|
|
88
|
+
["echo", "--text", "define once", "--loud"],
|
|
89
|
+
["echo", "--text", "ab", "--times", "3"],
|
|
90
|
+
["add", "--a", "1.5", "--b", "2"],
|
|
91
|
+
["now", "--zone", "UTC", "--utc-offset-hours", "8"],
|
|
92
|
+
["add", "--a", "oops", "--b", "2"],
|
|
93
|
+
]
|
|
94
|
+
|
|
95
|
+
_MCP_CALLS = [
|
|
96
|
+
("echo", {"text": "define once", "loud": True}),
|
|
97
|
+
("add", {"a": 1.5, "b": 2}),
|
|
98
|
+
("now", {"zone": "UTC", "utc_offset_hours": 8}),
|
|
99
|
+
]
|
|
100
|
+
|
|
101
|
+
|
|
102
|
+
def run_cli_demo() -> int:
|
|
103
|
+
"""Run the CLI exit through a real subprocess and print its output."""
|
|
104
|
+
import subprocess
|
|
105
|
+
import sys
|
|
106
|
+
|
|
107
|
+
root = _project_root()
|
|
108
|
+
print("=" * 72)
|
|
109
|
+
print("CLI exit: python -m appport_sdk.cli --tools examples.demo_tools:TOOLS")
|
|
110
|
+
print("=" * 72)
|
|
111
|
+
for command in CLI_COMMANDS:
|
|
112
|
+
argv = [
|
|
113
|
+
sys.executable,
|
|
114
|
+
"-m",
|
|
115
|
+
"appport_sdk.cli",
|
|
116
|
+
"--tools",
|
|
117
|
+
"examples.demo_tools:TOOLS",
|
|
118
|
+
*command,
|
|
119
|
+
]
|
|
120
|
+
print(
|
|
121
|
+
f"\nPS> appport-sdk --tools examples.demo_tools:TOOLS "
|
|
122
|
+
f"{' '.join(command)}"
|
|
123
|
+
)
|
|
124
|
+
completed = subprocess.run(
|
|
125
|
+
argv,
|
|
126
|
+
cwd=root,
|
|
127
|
+
capture_output=True,
|
|
128
|
+
text=True,
|
|
129
|
+
encoding="utf-8",
|
|
130
|
+
errors="replace",
|
|
131
|
+
)
|
|
132
|
+
print(completed.stdout.rstrip() or completed.stderr.rstrip())
|
|
133
|
+
print(f"[exit code: {completed.returncode}]")
|
|
134
|
+
return 0
|
|
135
|
+
|
|
136
|
+
|
|
137
|
+
async def _run_mcp_demo() -> int:
|
|
138
|
+
"""Start the MCP exit as a real stdio subprocess and call it."""
|
|
139
|
+
import os
|
|
140
|
+
import sys
|
|
141
|
+
|
|
142
|
+
from mcp import Client
|
|
143
|
+
from mcp.client.stdio import StdioServerParameters
|
|
144
|
+
|
|
145
|
+
root = _project_root()
|
|
146
|
+
env = dict(os.environ)
|
|
147
|
+
env["PYTHONPATH"] = os.pathsep.join(
|
|
148
|
+
[os.path.join(root, "src"), root, env.get("PYTHONPATH", "")]
|
|
149
|
+
).strip(os.pathsep)
|
|
150
|
+
params = StdioServerParameters(
|
|
151
|
+
command=sys.executable,
|
|
152
|
+
args=["-m", "appport_sdk.mcp_server", "--tools", "examples.demo_tools:TOOLS"],
|
|
153
|
+
env=env,
|
|
154
|
+
cwd=root,
|
|
155
|
+
)
|
|
156
|
+
|
|
157
|
+
print("\n" + "=" * 72)
|
|
158
|
+
print(
|
|
159
|
+
"MCP exit: python -m appport_sdk.mcp_server --tools examples.demo_tools:TOOLS"
|
|
160
|
+
)
|
|
161
|
+
print("=" * 72)
|
|
162
|
+
async with Client(params) as client:
|
|
163
|
+
info = client.server_info
|
|
164
|
+
print(
|
|
165
|
+
f"\nconnected to {info.name} {info.version} "
|
|
166
|
+
f"(protocol {client.protocol_version})"
|
|
167
|
+
)
|
|
168
|
+
listing = await client.list_tools()
|
|
169
|
+
print(f"\ntools/list -> {len(listing.tools)} tools")
|
|
170
|
+
for item in listing.tools:
|
|
171
|
+
schema = item.input_schema
|
|
172
|
+
required = schema.get("required", [])
|
|
173
|
+
print(f" - {item.name}: {item.description}")
|
|
174
|
+
print(
|
|
175
|
+
f" params={list(schema.get('properties', {}))} "
|
|
176
|
+
f"required={required}"
|
|
177
|
+
)
|
|
178
|
+
print("\ntools/call")
|
|
179
|
+
for name, arguments in _MCP_CALLS:
|
|
180
|
+
result = await client.call_tool(name, arguments)
|
|
181
|
+
text = result.content[0].text if result.content else ""
|
|
182
|
+
print(f" PS> {name}({arguments}) -> isError={result.is_error}")
|
|
183
|
+
for line in text.splitlines():
|
|
184
|
+
print(f" {line}")
|
|
185
|
+
return 0
|
|
186
|
+
|
|
187
|
+
|
|
188
|
+
def run_mcp_demo() -> int:
|
|
189
|
+
import asyncio
|
|
190
|
+
|
|
191
|
+
return asyncio.run(_run_mcp_demo())
|
|
192
|
+
|
|
193
|
+
|
|
194
|
+
def _project_root() -> str:
|
|
195
|
+
import pathlib
|
|
196
|
+
|
|
197
|
+
return str(pathlib.Path(__file__).resolve().parent.parent)
|
|
198
|
+
|
|
199
|
+
|
|
200
|
+
def demo() -> int:
|
|
201
|
+
"""Show one definition producing two working exits."""
|
|
202
|
+
print("AppPort SDK demo: one ToolDefinition, two exits\n")
|
|
203
|
+
print("Definitions (the single source of truth):")
|
|
204
|
+
for definition in TOOLS.definitions():
|
|
205
|
+
params = ", ".join(
|
|
206
|
+
f"{p.name}: {p.type}"
|
|
207
|
+
+ ("" if p.required else f" = {p.default!r}")
|
|
208
|
+
for p in definition.params
|
|
209
|
+
)
|
|
210
|
+
print(f" - {definition.name}({params}) -> {definition.returns.type}")
|
|
211
|
+
run_cli_demo()
|
|
212
|
+
run_mcp_demo()
|
|
213
|
+
print("\nBoth exits above ran from the same TOOLS registry.")
|
|
214
|
+
return 0
|
|
215
|
+
|
|
216
|
+
|
|
217
|
+
if __name__ == "__main__":
|
|
218
|
+
raise SystemExit(demo())
|
|
219
|
+
|
|
220
|
+
|
|
221
|
+
__all__ = [
|
|
222
|
+
"CLI_COMMANDS",
|
|
223
|
+
"TOOLS",
|
|
224
|
+
"add",
|
|
225
|
+
"demo",
|
|
226
|
+
"echo",
|
|
227
|
+
"now",
|
|
228
|
+
"run_cli_demo",
|
|
229
|
+
"run_mcp_demo",
|
|
230
|
+
]
|
|
@@ -0,0 +1,63 @@
|
|
|
1
|
+
[build-system]
|
|
2
|
+
requires = ["setuptools>=68"]
|
|
3
|
+
build-backend = "setuptools.build_meta"
|
|
4
|
+
|
|
5
|
+
[project]
|
|
6
|
+
name = "appport-sdk"
|
|
7
|
+
version = "0.1.0"
|
|
8
|
+
description = "Define once, expose twice: turn one Python tool definition into an MCP server and a CLI."
|
|
9
|
+
readme = "README.md"
|
|
10
|
+
requires-python = ">=3.11"
|
|
11
|
+
license = "MIT"
|
|
12
|
+
license-files = ["LICENSE"]
|
|
13
|
+
authors = [{ name = "AppPort contributors" }]
|
|
14
|
+
keywords = ["mcp", "cli", "tools", "llm", "json-schema"]
|
|
15
|
+
classifiers = [
|
|
16
|
+
"Programming Language :: Python :: 3",
|
|
17
|
+
"Programming Language :: Python :: 3.11",
|
|
18
|
+
"Programming Language :: Python :: 3.12",
|
|
19
|
+
"Operating System :: OS Independent",
|
|
20
|
+
]
|
|
21
|
+
dependencies = [
|
|
22
|
+
# Locked to the mcp 2.x line on purpose: the MCP exit uses the v2 low-level
|
|
23
|
+
# ``Server`` with explicit ``on_list_tools``/``on_call_tool`` handlers.
|
|
24
|
+
# 1.x's FastMCP API no longer exists in 2.x, and appport-bridge already runs
|
|
25
|
+
# on v2, so both repositories pin the same window to stay co-installable.
|
|
26
|
+
"mcp>=2,<3", # v2 low-level Server; FastMCP was removed in 2.0
|
|
27
|
+
# Imported directly by appport_sdk.mcp_server to drive the stdio loop.
|
|
28
|
+
# Floor matches mcp 2.x's own requirement so the two never disagree.
|
|
29
|
+
"anyio>=4.9",
|
|
30
|
+
"typer>=0.12",
|
|
31
|
+
]
|
|
32
|
+
|
|
33
|
+
[project.optional-dependencies]
|
|
34
|
+
# Kept separate so the schema tests can validate inputSchema without the
|
|
35
|
+
# runtime package paying for jsonschema.
|
|
36
|
+
test = [
|
|
37
|
+
"pytest>=8",
|
|
38
|
+
"jsonschema>=4",
|
|
39
|
+
]
|
|
40
|
+
|
|
41
|
+
[project.scripts]
|
|
42
|
+
appport-sdk = "appport_sdk.cli:main"
|
|
43
|
+
appport-sdk-mcp = "appport_sdk.mcp_server:main"
|
|
44
|
+
|
|
45
|
+
[project.urls]
|
|
46
|
+
Homepage = "https://github.com/RookieApe-tao/appport-sdk"
|
|
47
|
+
Companion = "https://github.com/RookieApe-tao/appport-bridge"
|
|
48
|
+
|
|
49
|
+
[tool.setuptools.packages.find]
|
|
50
|
+
where = ["src"]
|
|
51
|
+
|
|
52
|
+
[tool.pytest.ini_options]
|
|
53
|
+
testpaths = ["tests"]
|
|
54
|
+
pythonpath = ["src", "."]
|
|
55
|
+
addopts = "-ra"
|
|
56
|
+
|
|
57
|
+
[tool.ruff]
|
|
58
|
+
line-length = 88
|
|
59
|
+
target-version = "py311"
|
|
60
|
+
src = ["src", "tests"]
|
|
61
|
+
|
|
62
|
+
[tool.ruff.lint]
|
|
63
|
+
select = ["E", "F"]
|