-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathcli_new.py
More file actions
130 lines (110 loc) · 4.65 KB
/
cli_new.py
File metadata and controls
130 lines (110 loc) · 4.65 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
import os
import shutil
from typing import List, Optional, Tuple
import click
from uipath._cli._utils._common import clean_directory
from uipath._cli._utils._console import ConsoleLogger
from uipath._cli.middlewares import MiddlewareResult
console = ConsoleLogger()
def write_template_file(
target_directory: str,
file_path: str,
file_name: str,
replace_tuple: Optional[List[Tuple[str, str]]] = None,
) -> None:
"""Write a template file to the target directory with optional placeholder replacements.
Args:
target_directory (str): Directory where the file will be created.
file_path (str): Path to the template file relative to this module.
file_name (str): Name of the file to create in the target directory.
replace_tuple (Optional[List[Tuple[str, str]]]): List of (placeholder, value) pairs for template substitution.
If None, the template file is copied as-is.
This function copies a template file to the target directory and optionally replaces placeholders
with specified values. It logs a success message after creating the file.
"""
template_path = os.path.join(os.path.dirname(__file__), file_path)
target_path = os.path.join(target_directory, file_name)
if replace_tuple is not None:
# replace the template placeholders
with open(template_path, "r", encoding="utf-8") as f:
template_content = f.read()
for target, new_value in replace_tuple:
template_content = template_content.replace(target, new_value)
with open(target_path, "w", encoding="utf-8") as f:
f.write(template_content)
else:
shutil.copyfile(template_path, target_path)
console.success(f"Created '{file_name}' file.")
def generate_files(target_directory: str, server_name: str):
"""Generate all necessary files for a new MCP server project.
Args:
target_directory (str): Directory where the files will be created.
server_name (str): Name of the MCP server, used in configuration files.
This function creates three essential files for an MCP server:
- server.py: The main server implementation
- mcp.json: Server configuration file
- pyproject.toml: Project metadata and dependencies
"""
write_template_file(
target_directory, "_templates/server.py.template", "server.py", None
)
write_template_file(
target_directory,
"_templates/mcp.json.template",
"mcp.json",
[("$server_name", server_name)],
)
write_template_file(
target_directory,
"_templates/pyproject.toml.template",
"pyproject.toml",
[("$project_name", server_name)],
)
def mcp_new_middleware(name: str) -> MiddlewareResult:
"""Create a new MCP server project with template files.
Args:
name (str): Name of the MCP server to create.
Returns:
MiddlewareResult: Result object indicating success/failure and whether to continue processing.
This middleware function:
1. Cleans the current directory of Python files
2. Generates new template files for the MCP server
3. Displays helpful instructions for initializing and running the server
4. Returns a MiddlewareResult indicating whether to continue processing
If an error occurs during creation, it returns a MiddlewareResult with should_include_stacktrace=True.
"""
directory = os.getcwd()
try:
with console.spinner(
f"Creating new mcp server '{name}' in current directory ..."
):
clean_directory(directory)
generate_files(directory, name)
init_command = """uipath init"""
run_command = f"""uipath run {name}"""
console.hint(
f""" Initialize project: {click.style(init_command, fg="cyan")}"""
)
line = click.style("═" * 60, bold=True)
console.info(line)
console.info(
click.style(
f"""Start '{name}' as a self-hosted MCP server""",
fg="magenta",
bold=True,
)
)
console.info(line)
console.hint(
f""" 1. Set {click.style("UIPATH_FOLDER_PATH", fg="cyan")} environment variable"""
)
console.hint(
f""" 2. Start the server locally: {click.style(run_command, fg="cyan")}"""
)
return MiddlewareResult(should_continue=False)
except Exception as e:
console.error(f"Error creating demo agent {str(e)}")
return MiddlewareResult(
should_continue=False,
should_include_stacktrace=True,
)