# PR Description
Consider this PR the result of a full pass through of this repository.
## Add helper for adding tools to an `MCPApp`
You can now add all of the tools in a module to an `MCPApp` via
`app.add_tools_from_module(...)`
## Edit what `arcade new` generates
First, I updated the backend to use hatchling.
Second, the structure generated before this PR was simple, but did not
create a proper Python module.
This hindered developers in the following ways:
1. Difficult to add the tools in your server to an evaluation suite
2. Difficult to add more than one tool to an MCPApp at a time
3. All other niceties that come with being able to import modules
```
# Before
server/
├── .env.example
├── server.py
└── pyproject.toml
```
This PR updates the structure generated such that a valid Python module
is generated:
```
# After
server/
├── pyproject.toml
└── src/
└── server/
├── __init__.py
├── .env.example
└── server.py
```
## Fix Tool Chaining
`self._ctx.server.executor.run(...)` was being called, but `MCPServer`
does not have an instance of `ToolExecutor` (and it's not intended to be
an instance anyways). I updated `Tool.call_raw` to pass the programmatic
tool call through the `MCPServer._handle_call_tool`. This means that the
programmatic tool calls now go through the same steps that a typical
tool call (initiated by the MCP client) would.
This means that **toolA**, which specifies **requirementsA**, is
permitted to call **toolB**, which specifies **requirementsB**, without
needing to explicitly declare or satisfy **requirementsB**. I believe
this is acceptable because the secrets and/or auth token associated with
**toolB's** `Context` are not exposed to **toolA**, and the secrets
and/or auth token associated with **toolA's** `Context` are not exposed
to **toolB**.
## Fix User Elicitation
1. The read & write streams were created with a maximum queue size of 0.
I increased this to 100.
2. I updated `ServerSession`'s run loop to both read messages from the
stream & process them concurrently. This enables server initiated
requests (like user elicitation and progress reporting) to be handled
while tools are being executed. Otherwise, the server initiated requests
would wait for the tool to finish executing and the tool execution would
wait for the server initiated request to finish.
3.
## Fix Progress Reporting
Progress tokens sent by the client were not being stored. Therefore
there was no way to notify a client with progress updates. I am now
storing the `progressToken`, along with other `_meta` sent from the
client, in the `ServerSession`'s `_request_meta`. I am setting
`_request_meta` whenever the `MCPServer` is handling an incoming message
from a client.
## Fix handling of server names with spaces
Before:
Server name: "The simple server name"
Tool name: whisper_secret
Name seen by client: "The_simple_server_name_WhisperSecret"
After
Server name: "The simple server name"
Tool name: whisper_secret
Name seen by client: "TheSimpleServerName_WhisperSecret"
## Add Integration Tests
The stdio integration test is much more comprehensive than the http
integration test. These tests will let me sleep a bit more at night
## Add Example MCP Servers
Example servers for sampling, user-elicitation, progress reporting,
logging, tool chaining, combining prebuilt tools with custom tools, tool
secrets, tool auth, evaluations, and more!
## Add Docker template
Added a Docker template for running an MCP server in Docker (and removed
the old docker stuff)
279 lines
9.6 KiB
Python
279 lines
9.6 KiB
Python
import re
|
|
import shutil
|
|
from datetime import datetime
|
|
from importlib.metadata import version as get_version
|
|
from pathlib import Path
|
|
from typing import Optional
|
|
|
|
import typer
|
|
from jinja2 import Environment, FileSystemLoader, select_autoescape
|
|
from rich.console import Console
|
|
|
|
from arcade_cli.templates import get_full_template_directory, get_minimal_template_directory
|
|
|
|
console = Console()
|
|
|
|
# Retrieve the installed version of arcade-mcp
|
|
try:
|
|
ARCADE_MCP_MIN_VERSION = get_version("arcade-mcp")
|
|
ARCADE_MCP_MAX_VERSION = str(int(ARCADE_MCP_MIN_VERSION.split(".")[0]) + 1) + ".0.0"
|
|
except Exception as e:
|
|
console.print(f"[red]Failed to get arcade-mcp version: {e}[/red]")
|
|
ARCADE_MCP_MIN_VERSION = "1.4.0" # Default version if unable to fetch
|
|
ARCADE_MCP_MAX_VERSION = "2.0.0"
|
|
|
|
ARCADE_TDK_MIN_VERSION = "3.0.0"
|
|
ARCADE_TDK_MAX_VERSION = "4.0.0"
|
|
ARCADE_SERVE_MIN_VERSION = "3.0.0"
|
|
ARCADE_SERVE_MAX_VERSION = "4.0.0"
|
|
ARCADE_MCP_SERVER_MIN_VERSION = "1.5.0"
|
|
ARCADE_MCP_SERVER_MAX_VERSION = "2.0.0"
|
|
|
|
|
|
def ask_question(question: str, default: Optional[str] = None) -> str:
|
|
"""
|
|
Ask a question via input() and return the answer.
|
|
"""
|
|
answer = typer.prompt(question, default=default, show_default=False)
|
|
if not answer and default:
|
|
return default
|
|
return str(answer)
|
|
|
|
|
|
def ask_yes_no_question(question: str, default: bool = True) -> bool:
|
|
"""
|
|
Ask a yes/no question via input() and return the bool answer.
|
|
"""
|
|
default_str = "Y/n" if default else "y/N"
|
|
answer = typer.prompt(
|
|
f"{question} ({default_str})", default="y" if default else "n", show_default=False
|
|
)
|
|
return answer.lower() in [
|
|
"y",
|
|
"y/",
|
|
"yes",
|
|
"true",
|
|
"1",
|
|
"ye",
|
|
"yes",
|
|
"yeah",
|
|
"yep",
|
|
"sure",
|
|
"ok",
|
|
"yup",
|
|
]
|
|
|
|
|
|
def render_template(env: Environment, template_string: str, context: dict) -> str:
|
|
"""Render a template string with the given variables."""
|
|
template = env.from_string(template_string)
|
|
return template.render(context)
|
|
|
|
|
|
def write_template(path: Path, content: str) -> None:
|
|
"""Write content to a file."""
|
|
path.write_text(content, encoding="utf-8")
|
|
|
|
|
|
def create_ignore_pattern(
|
|
include_evals: bool, is_community_or_official_toolkit: bool
|
|
) -> re.Pattern[str]:
|
|
"""Create an ignore pattern based on user preferences."""
|
|
patterns = [
|
|
"__pycache__",
|
|
r"\.DS_Store",
|
|
r"Thumbs\.db",
|
|
r"\.git",
|
|
r"\.svn",
|
|
r"\.hg",
|
|
r"\.vscode",
|
|
r"\.idea",
|
|
"build",
|
|
"dist",
|
|
r".*\.egg-info",
|
|
r".*\.pyc",
|
|
r".*\.pyo",
|
|
]
|
|
|
|
if not include_evals:
|
|
patterns.append("evals")
|
|
|
|
if not is_community_or_official_toolkit:
|
|
patterns.extend([".ruff.toml", ".pre-commit-config.yaml", "LICENSE"])
|
|
else:
|
|
patterns.extend(["README.md"])
|
|
|
|
return re.compile(f"({'|'.join(patterns)})$")
|
|
|
|
|
|
def create_package(
|
|
env: Environment,
|
|
template_path: Path,
|
|
output_path: Path,
|
|
context: dict,
|
|
ignore_pattern: re.Pattern[str],
|
|
) -> None:
|
|
"""Recursively create a new toolkit directory structure from jinja2 templates."""
|
|
if ignore_pattern.match(template_path.name):
|
|
return
|
|
|
|
try:
|
|
if template_path.is_dir():
|
|
folder_name = render_template(env, template_path.name, context)
|
|
new_dir_path = output_path / folder_name
|
|
new_dir_path.mkdir(parents=True, exist_ok=True)
|
|
|
|
for item in template_path.iterdir():
|
|
create_package(env, item, new_dir_path, context, ignore_pattern)
|
|
|
|
else:
|
|
# Render the file name
|
|
file_name = render_template(env, template_path.name, context)
|
|
with open(template_path, encoding="utf-8") as f:
|
|
content = f.read()
|
|
# Render the file content
|
|
content = render_template(env, content, context)
|
|
|
|
write_template(output_path / file_name, content)
|
|
except Exception as e:
|
|
console.print(f"[red]Failed to create package: {e}[/red]")
|
|
raise
|
|
|
|
|
|
def remove_toolkit(toolkit_directory: Path, toolkit_name: str) -> None:
|
|
"""Teardown logic for when creating a new toolkit fails."""
|
|
toolkit_path = toolkit_directory / toolkit_name
|
|
if toolkit_path.exists():
|
|
shutil.rmtree(toolkit_path)
|
|
|
|
|
|
def create_new_toolkit(output_directory: str, toolkit_name: str) -> None:
|
|
"""Create a new toolkit from a template with user input."""
|
|
toolkit_directory = Path(output_directory)
|
|
|
|
# Check for illegal characters in the toolkit name
|
|
if re.match(r"^[a-z0-9_]+$", toolkit_name):
|
|
if (toolkit_directory / toolkit_name).exists():
|
|
console.print(f"[red]Server '{toolkit_name}' already exists.[/red]")
|
|
exit(1)
|
|
else:
|
|
console.print(
|
|
"[red]Server name contains illegal characters. "
|
|
"Only lowercase alphanumeric characters and underscores are allowed. "
|
|
"Please try again.[/red]"
|
|
)
|
|
exit(1)
|
|
|
|
toolkit_description = ask_question("Describe what your server will do (optional)", default="")
|
|
toolkit_author_name = ask_question("Your GitHub username (optional)", default="")
|
|
while True:
|
|
toolkit_author_email = ask_question("Your email (optional)", default="")
|
|
if toolkit_author_email == "" or re.match(r"[^@ ]+@[^@ ]+\.[^@ ]+", toolkit_author_email):
|
|
break
|
|
console.print(
|
|
"[red]Invalid email format. Please enter a valid email address or leave it empty.[/red]"
|
|
)
|
|
include_evals = ask_yes_no_question(
|
|
"Do you want an evals directory created for you?", default=True
|
|
)
|
|
|
|
cwd = Path.cwd()
|
|
# TODO: this detection mechanism works only for people that didn't change the
|
|
# name of the repo, a better detection method is required here
|
|
is_community_toolkit = False
|
|
if cwd.name == "toolkits" and cwd.parent.name == "arcade-mcp":
|
|
prompt = (
|
|
"Is your server a community contribution (to be merged into "
|
|
"\x1b]8;;https://github.com/ArcadeAI/arcade-mcp\x1b\\ArcadeAI/arcade-mcp\x1b]8;;\x1b\\ repo)?"
|
|
)
|
|
is_community_toolkit = ask_yes_no_question(prompt, default=True)
|
|
|
|
is_official_toolkit = cwd.name == "toolkits" and cwd.parent.name == "tools"
|
|
|
|
context = {
|
|
"package_name": "arcade_" + toolkit_name if is_community_toolkit else toolkit_name,
|
|
"toolkit_name": toolkit_name,
|
|
"toolkit_description": toolkit_description,
|
|
"toolkit_author_name": toolkit_author_name,
|
|
"toolkit_author_email": toolkit_author_email,
|
|
"arcade_tdk_min_version": ARCADE_TDK_MIN_VERSION,
|
|
"arcade_tdk_max_version": ARCADE_TDK_MAX_VERSION,
|
|
"arcade_serve_min_version": ARCADE_SERVE_MIN_VERSION,
|
|
"arcade_serve_max_version": ARCADE_SERVE_MAX_VERSION,
|
|
"arcade_mcp_min_version": ARCADE_MCP_MIN_VERSION,
|
|
"arcade_mcp_max_version": ARCADE_MCP_MAX_VERSION,
|
|
"creation_year": datetime.now().year,
|
|
"is_community_toolkit": is_community_toolkit,
|
|
"is_official_toolkit": is_official_toolkit,
|
|
}
|
|
|
|
template_directory = get_full_template_directory() / "{{ toolkit_name }}"
|
|
|
|
env = Environment(
|
|
loader=FileSystemLoader(str(template_directory)),
|
|
autoescape=select_autoescape(["html", "xml"]),
|
|
)
|
|
|
|
# Create dynamic ignore pattern based on user preferences
|
|
ignore_pattern = create_ignore_pattern(
|
|
include_evals, is_community_toolkit or is_official_toolkit
|
|
)
|
|
|
|
try:
|
|
create_package(env, template_directory, toolkit_directory, context, ignore_pattern)
|
|
console.print(
|
|
f"[green]Toolkit '{toolkit_name}' created successfully at '{toolkit_directory}'.[/green]"
|
|
)
|
|
create_deployment(toolkit_directory, toolkit_name)
|
|
except Exception:
|
|
remove_toolkit(toolkit_directory, toolkit_name)
|
|
raise
|
|
|
|
|
|
def create_deployment(toolkit_directory: Path, toolkit_name: str) -> None:
|
|
# No longer create worker.toml for MCP servers
|
|
# The server.py file handles all configuration
|
|
pass
|
|
|
|
|
|
def create_new_toolkit_minimal(output_directory: str, toolkit_name: str) -> None:
|
|
"""Create a new toolkit from a template with user input."""
|
|
toolkit_directory = Path(output_directory)
|
|
|
|
# Check for illegal characters in the toolkit name
|
|
if re.match(r"^[a-z0-9_]+$", toolkit_name):
|
|
if (toolkit_directory / toolkit_name).exists():
|
|
raise FileExistsError(
|
|
f"Server with name '{toolkit_name}' already exists at '{toolkit_directory / toolkit_name}'"
|
|
)
|
|
else:
|
|
raise ValueError(
|
|
f"Server name '{toolkit_name}' contains illegal characters. "
|
|
"Only lowercase alphanumeric characters and underscores are allowed. "
|
|
"Please try again."
|
|
)
|
|
|
|
context = {
|
|
"toolkit_name": toolkit_name,
|
|
"arcade_mcp_min_version": ARCADE_MCP_MIN_VERSION,
|
|
"arcade_mcp_max_version": ARCADE_MCP_MAX_VERSION,
|
|
"arcade_mcp_server_min_version": ARCADE_MCP_SERVER_MIN_VERSION,
|
|
"arcade_mcp_server_max_version": ARCADE_MCP_SERVER_MAX_VERSION,
|
|
}
|
|
template_directory = get_minimal_template_directory() / "{{ toolkit_name }}"
|
|
|
|
env = Environment(
|
|
loader=FileSystemLoader(str(template_directory)),
|
|
autoescape=select_autoescape(["html", "xml"]),
|
|
)
|
|
|
|
ignore_pattern = create_ignore_pattern(False, False)
|
|
|
|
try:
|
|
create_package(env, template_directory, toolkit_directory, context, ignore_pattern)
|
|
console.print(
|
|
f"[green]Server '{toolkit_name}' created successfully at '{toolkit_directory}'.[/green]"
|
|
)
|
|
except Exception:
|
|
remove_toolkit(toolkit_directory, toolkit_name)
|
|
raise
|