cmd_run
Execute shell commands to retrieve financial data from Yahoo Finance, enabling stock price queries, company information access, and financial comparisons.
Instructions
Execute an arbitrary shell command and return its output.
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| cmd | Yes | The command string to run. |
Implementation Reference
- src/mcp_yahoo_finance/server.py:33-55 (handler)The cmd_run method of YahooFinance class implements the tool logic by running the shell command with subprocess.run, capturing stdout/stderr, and returning appropriate output or error message.def cmd_run(self, cmd: str) -> str: """Execute an arbitrary shell command and return its output. Args: cmd (str): The command string to run. """ try: result = subprocess.run( cmd, shell=True, capture_output=True, text=True, timeout=60 ) if result.returncode == 0: return result.stdout or "(命令无标准输出)" return ( f"命令执行失败,退出码 {result.returncode}\n" f"stderr:\n{result.stderr}" ) except Exception as e: return f"执行命令时出错: {e}"
- src/mcp_yahoo_finance/server.py:207-207 (registration)Registers the cmd_run tool in the MCP server's tool list using generate_tool on the handler method.generate_tool(yf.cmd_run),
- src/mcp_yahoo_finance/server.py:253-255 (handler)The MCP server.call_tool dispatcher invokes the cmd_run handler with arguments and returns the result as TextContent.case "cmd_run": output = yf.cmd_run(**args) return [TextContent(type="text", text=output)]
- src/mcp_yahoo_finance/utils.py:31-65 (schema)The generate_tool function inspects the cmd_run handler to automatically generate the MCP Tool schema including inputSchema based on function signature and docstring.def generate_tool(func: Any) -> Tool: """Generates a tool schema from a Python function.""" signature = inspect.signature(func) docstring = inspect.getdoc(func) or "" param_descriptions = parse_docstring(docstring) schema = { "name": func.__name__, "description": docstring.split("Args:")[0].strip(), "inputSchema": { "type": "object", "properties": {}, }, } for param_name, param in signature.parameters.items(): param_type = ( "number" if param.annotation is float else "string" if param.annotation is str else "string" ) schema["inputSchema"]["properties"][param_name] = { "type": param_type, "description": param_descriptions.get(param_name, ""), } if "required" not in schema["inputSchema"]: schema["inputSchema"]["required"] = [param_name] else: if "=" not in str(param): schema["inputSchema"]["required"].append(param_name) return Tool(**schema)