turbomcp 0.2.0__py3-none-any.whl

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.
turbomcp/__init__.py ADDED
@@ -0,0 +1,7 @@
1
+ """turbomcp — a zero-dependency tool registry and MCP server for LLM agents."""
2
+
3
+ from .core import ContextAware
4
+ from .mcp_stdio import serve_stdio
5
+
6
+ __version__ = "0.2.0"
7
+ __all__ = ["ContextAware", "serve_stdio", "__version__"]
turbomcp/__main__.py ADDED
@@ -0,0 +1,173 @@
1
+ """turbomcp CLI.
2
+
3
+ Usage:
4
+ turbomcp serve my_tools.py # serve a registry over MCP stdio
5
+ turbomcp serve my_tools.py:registry # name the registry variable
6
+ turbomcp serve my_tools.py --lazy # progressive-disclosure mode
7
+ turbomcp demo # serve the built-in default tools
8
+ turbomcp generate-token # token for the optional HTTP API
9
+ turbomcp init [my_tools.py] # create a starter template
10
+ """
11
+
12
+ import argparse
13
+ import importlib.util
14
+ import os
15
+ import sys
16
+ from hashlib import sha256
17
+
18
+ from .core import ContextAware
19
+ from .mcp_stdio import serve_stdio
20
+
21
+
22
+ _TEMPLATE = '''"""Example tool registry for turbomcp.
23
+
24
+ Run with: turbomcp serve my_tools.py
25
+ Or with lazy mode: turbomcp serve my_tools.py --lazy
26
+ """
27
+
28
+ from turbomcp import ContextAware
29
+
30
+ mcp = ContextAware(name="my-tools")
31
+
32
+
33
+ @mcp.tool()
34
+ def greet(name: str = "World") -> str:
35
+ """Greet a user by name.
36
+
37
+ Args:
38
+ name (str): Who to greet
39
+ """
40
+ return f"Hello, {name}!"
41
+
42
+
43
+ @mcp.tool()
44
+ def add(a: int, b: int) -> int:
45
+ """Add two integers.
46
+
47
+ Args:
48
+ a (int): First number
49
+ b (int): Second number
50
+ """
51
+ return a + b
52
+ '''
53
+
54
+ _TEMPLATE_WEB = '''
55
+
56
+ # Optional: register built-in web tools (requires pip install turbomcp[web])
57
+ mcp.register_default(all=True)
58
+ # Or pick specific ones:
59
+ # mcp.register_default(names=["internet_search", "webpage_scrape", "calculate", "read_file", "get_current_time"])
60
+ '''
61
+
62
+ _TEMPLATE_SEMANTIC = '''
63
+
64
+ # Optional: enable semantic tool search (requires pip install turbomcp[semantic])
65
+ mcp.enable_semantic_search()
66
+ '''
67
+
68
+ _TEMPLATE_MAIN = '''
69
+
70
+ if __name__ == "__main__":
71
+ # This allows: python my_tools.py
72
+ from turbomcp.mcp_stdio import serve_stdio
73
+ serve_stdio(mcp)
74
+ '''
75
+
76
+
77
+ def _build_template(with_web=False, with_semantic=False):
78
+ """Build the template based on options."""
79
+ parts = [_TEMPLATE]
80
+ if with_web:
81
+ parts.append(_TEMPLATE_WEB)
82
+ if with_semantic:
83
+ parts.append(_TEMPLATE_SEMANTIC)
84
+ parts.append(_TEMPLATE_MAIN)
85
+ return "".join(parts)
86
+
87
+
88
+ def _load_registry(target):
89
+ """Load 'path/to/file.py[:varname]' and return its ContextAware instance."""
90
+ path, _, var = target.partition(":")
91
+ path = os.path.abspath(path)
92
+ if not os.path.isfile(path):
93
+ sys.exit(f"turbomcp: no such file: {path}")
94
+
95
+ spec = importlib.util.spec_from_file_location("_turbomcp_target", path)
96
+ module = importlib.util.module_from_spec(spec)
97
+ spec.loader.exec_module(module)
98
+
99
+ if var:
100
+ registry = getattr(module, var, None)
101
+ if not isinstance(registry, ContextAware):
102
+ sys.exit(f"turbomcp: '{var}' in {path} is not a ContextAware instance")
103
+ return registry
104
+
105
+ found = [v for v in vars(module).values() if isinstance(v, ContextAware)]
106
+ if not found:
107
+ sys.exit(f"turbomcp: no ContextAware instance found in {path}")
108
+ if len(found) > 1:
109
+ sys.exit(f"turbomcp: multiple registries in {path}; use {path}:varname")
110
+ return found[0]
111
+
112
+
113
+ def main(argv=None):
114
+ parser = argparse.ArgumentParser(prog="turbomcp")
115
+ sub = parser.add_subparsers(dest="cmd", required=True)
116
+
117
+ p_serve = sub.add_parser("serve", help="Serve a registry over MCP stdio")
118
+ p_serve.add_argument("target", help="path/to/file.py[:varname] defining a ContextAware registry")
119
+ p_serve.add_argument("--lazy", action="store_true",
120
+ help="Expose only search/get/call meta-tools; the client discovers the catalog via search")
121
+
122
+ p_demo = sub.add_parser("demo", help="Serve the built-in default tools over MCP stdio")
123
+ p_demo.add_argument("--lazy", action="store_true")
124
+
125
+ sub.add_parser("generate-token", help="Print a random token for the optional HTTP API")
126
+
127
+ p_init = sub.add_parser("init", help="Create a starter tool registry template")
128
+ p_init.add_argument("target", nargs="?", default="my_tools.py",
129
+ help="Output file path (default: my_tools.py)")
130
+ p_init.add_argument("--force", action="store_true", help="Overwrite existing file")
131
+ p_init.add_argument("--with-web", action="store_true",
132
+ help="Include web tools (internet_search, webpage_scrape) in template")
133
+ p_init.add_argument("--with-semantic", action="store_true",
134
+ help="Include semantic search enablement in template")
135
+
136
+ sub.add_parser("help", help="Show this help message and exit")
137
+
138
+ args = parser.parse_args(argv)
139
+
140
+ if args.cmd == "generate-token":
141
+ print(sha256(os.urandom(16)).hexdigest())
142
+ return
143
+
144
+ if args.cmd == "demo":
145
+ registry = ContextAware(name="turbomcp-demo")
146
+ registry.register_default(all=True)
147
+ serve_stdio(registry, lazy=args.lazy)
148
+ return
149
+
150
+ if args.cmd == "init":
151
+ target = os.path.abspath(args.target)
152
+ if os.path.exists(target) and not args.force:
153
+ sys.exit(f"turbomcp: file exists: {target} (use --force to overwrite)")
154
+ template = _build_template(with_web=args.with_web, with_semantic=args.with_semantic)
155
+ with open(target, "w", encoding="utf-8") as f:
156
+ f.write(template)
157
+ print(f"Created {target}")
158
+ if args.with_web:
159
+ print(" (includes web tools - requires: pip install turbomcp[web])")
160
+ if args.with_semantic:
161
+ print(" (includes semantic search - requires: pip install turbomcp[semantic])")
162
+ print("Run it with: turbomcp serve " + args.target)
163
+ return
164
+
165
+ if args.cmd == "help":
166
+ parser.print_help()
167
+ return
168
+
169
+ serve_stdio(_load_registry(args.target), lazy=args.lazy)
170
+
171
+
172
+ if __name__ == "__main__":
173
+ main()