-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathcli.py
More file actions
142 lines (120 loc) · 3.89 KB
/
cli.py
File metadata and controls
142 lines (120 loc) · 3.89 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
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
#!/usr/bin/env python3
import argparse
from mcp_openapi.parser import Spec
from mcp_openapi.tools import (
tools_from_spec,
get_tool_function_body,
create_tool_function_exec,
)
def parse_command(args: argparse.Namespace) -> None:
"""Handle the parse command."""
if args.file and args.url:
print("Error: Cannot specify both --file and --url")
return
if not args.file and not args.url:
print("Error: Must specify either --file or --url")
return
if args.file:
spec = Spec.from_file(
args.file,
args.paths,
use_cache=False,
)
else:
spec = Spec.from_url(args.url, args.paths, use_cache=False)
print(spec)
def tools_command(args: argparse.Namespace) -> None:
"""Handle the tools command."""
if args.file and args.url:
print("Error: Cannot specify both --file and --url")
return
if not args.file and not args.url:
print("Error: Must specify either --file or --url")
return
if args.file:
spec = Spec.from_file(
args.file,
args.paths,
use_cache=False,
)
else:
spec = Spec.from_url(args.url, args.paths, use_cache=False)
print("\n----- Tools Definitions -----\n")
tools = tools_from_spec(spec, args.forward_query_params)
for tool in tools:
print(f"Tool: {tool.name}")
print(f"Description: {tool.description}")
print(f"Method: {tool.method}")
print(f"Path: {tool.path}")
print("Query Params:")
for param in tool.query_params:
print(f" - {param.name}: {param.type}")
if param.description:
print(f" Description: {param.description}")
for content_type, params in tool.body_by_content_type.items():
print(f"Body Params ({content_type}):")
for param in params:
print(f" - {param.name}: {param.type}")
if param.description:
print(f" Description: {param.description}")
if param.request_body_field:
print(f" Request Body Field: {param.request_body_field}")
print("\n----- Tool Functions -----\n")
for tool in tools:
print(get_tool_function_body(tool) + "\n")
# Ensure the tool functions are valid python. Throws an exception if not.
for tool in tools:
create_tool_function_exec(tool)
def main() -> None:
parser = argparse.ArgumentParser(description="MCP OpenAPI CLI tools")
subparsers = parser.add_subparsers(dest="command", help="Available commands")
# Parse command
parse_parser = subparsers.add_parser("parse", help="Parse an OpenAPI spec")
parse_parser.add_argument(
"--file",
help="Path to OpenAPI spec file",
type=str,
)
parse_parser.add_argument(
"--url",
help="URL to OpenAPI spec",
type=str,
)
parse_parser.add_argument(
"--paths",
nargs="+",
required=True,
help="Route patterns to include (regex)",
)
# Tools command
tools_parser = subparsers.add_parser("tools", help="Display tool definitions")
tools_parser.add_argument(
"--file",
help="Path to OpenAPI spec file",
type=str,
)
tools_parser.add_argument(
"--url",
help="URL to OpenAPI spec",
type=str,
)
tools_parser.add_argument(
"--paths",
nargs="+",
required=True,
help="Route patterns to include (regex)",
)
tools_parser.add_argument(
"--forward-query-params",
nargs="+",
help="Query parameters to forward to the tool",
)
args = parser.parse_args()
if args.command == "parse":
parse_command(args)
elif args.command == "tools":
tools_command(args)
else:
parser.print_help()
if __name__ == "__main__":
main()