Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

mcp: add back uv hardcode + simplify udf execution #732

Merged
merged 2 commits into from
Mar 18, 2025
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 7 additions & 4 deletions public/common/utils.py
Original file line number Diff line number Diff line change
@@ -4,7 +4,6 @@
import fused
import pandas as pd
import numpy as np
import sys
import json
import os
from numpy.typing import NDArray
@@ -2905,13 +2904,14 @@ def save_to_agent(
# save agent.json
json.dump(agent_json, open(agent_json_path, "w"), indent=4)

def generate_local_mcp_config(config_path: str, agents_list: list[str], repo_path: str, script_path: str = 'run.py'):
def generate_local_mcp_config(config_path: str, agents_list: list[str], repo_path: str, uv_path: str = 'uv', script_path: str = 'main.py'):
"""
Generate MCP configuration file based on list of agents from the udf_ai directory
Args:
config_path (str): Absolute path to the MCP configuration file.
agents_list (list[str]): List of agent names to be included in the configuration.
repo_path (str): Absolute path to the locally cloned udf_ai repo directory.
uv_path (str): Path to `uv`. Defaults to `uv` but might require your local path to `uv`.
script_path (str): Path to the script to run. Defaults to `run.py`.
"""
if not os.path.exists(repo_path):
@@ -2932,9 +2932,12 @@ def generate_local_mcp_config(config_path: str, agents_list: list[str], repo_pat
raise ValueError(f"No UDFs found for agent {agent_name}")

agent_config = {
"command": sys.executable,
"command": uv_path,
"args": [
f"{repo_path}/{script_path}",
"run",
"--directory",
f"{repo_path}",
f"{script_path}",
"--runtime=local",
f"--udf-names={','.join(agent['udfs'])}",
f"--name={agent_name}",
29 changes: 6 additions & 23 deletions public/common_mcp/utils.py
Original file line number Diff line number Diff line change
@@ -34,16 +34,15 @@
def register_udf_tool(mcp: FastMCP, udf: AnyBaseUdf):
"""Register a UDF tool using provided metadata."""
mcp_metadata = udf.metadata.get("fused:mcp")
udf_name = udf.metadata.get("fused:name")

if not mcp_metadata:
raise ValueError(
f"No metadata provided for token '{udf_name}', skipping registration"
f"No metadata provided for token '{udf.name}', skipping registration"
)

# Log the parameters from metadata
logger.info(
f"Registering tool '{udf_name}' with parameters: {mcp_metadata['parameters']}"
f"Registering tool '{udf.name}' with parameters: {mcp_metadata['parameters']}"
)

# Create docstring from metadata
@@ -58,32 +57,16 @@ def register_udf_tool(mcp: FastMCP, udf: AnyBaseUdf):
)
else:
logger.info(
f"No parameters received for {udf_name} so skipping creating docstring for parameters"
f"No parameters received for {udf.name} so skipping creating docstring for parameters"
)

async def udf_tool(**params):
"""Dynamic UDF tool implementation."""
# Log what parameters were received
logger.info(f"Tool '{udf_name}' received parameters: {params}")

# Initialize API parameters dict
api_params = {}

# Copy all parameters directly to the API parameters
try:
for key, value in params.items():
if key != "return_code": # Skip non-API parameters
api_params[key] = value
logger.info(f"Added parameter: {key}={value}")
except Exception as e:
api_params = {}
logger.info(f"No parameters received for {udf_name}")

# Log the final parameters
logger.info(f"Final API parameters for {udf_name}: {api_params}")
logger.info(f"Tool '{udf.name}' received parameters: {params}")

try:
return fused.run(udf, **api_params)
return fused.run(udf, **params)
except Exception as e:
logger.error(f"Error executing function: {e}")
return f"Error: {str(e)}"
@@ -92,7 +75,7 @@ async def udf_tool(**params):
udf_tool.__doc__ = docstring

# Register the tool with the token ID as name
return mcp.tool(name=udf_name)(udf_tool)
return mcp.tool(name=udf.name)(udf_tool)


def create_server_from_token_ids(