cmd_run
Run shell commands directly and retrieve outputs using the cmd_run tool, part of the MCP Yahoo Finance server, to streamline data retrieval and processing tasks.
Instructions
Execute an arbitrary shell command and return its output.
Input Schema
| Name | Required | Description | Default |
|---|---|---|---|
| cmd | Yes | The command string to run. |
Input Schema (JSON Schema)
{
"properties": {
"cmd": {
"description": "The command string to run.",
"type": "string"
}
},
"required": [
"cmd"
],
"type": "object"
}
Implementation Reference
- src/mcp_yahoo_finance/server.py:33-54 (handler)Core implementation of the cmd_run tool. Runs the shell command via subprocess, captures stdout/stderr, handles success and failure cases with timeout.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)Tool registration via generate_tool call in the @server.list_tools() handler, adding cmd_run to the available tools list.generate_tool(yf.cmd_run),
- src/mcp_yahoo_finance/utils.py:31-65 (schema)Dynamic schema generation for cmd_run tool (and others) from function signature, type annotations, and docstring parsing. Defines inputSchema with 'cmd' as required string property.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)
- src/mcp_yahoo_finance/server.py:253-255 (handler)Dispatch handler in @server.call_tool() that invokes the cmd_run method and formats the result as TextContent for MCP response.case "cmd_run": output = yf.cmd_run(**args) return [TextContent(type="text", text=output)]
- src/mcp_yahoo_finance/utils.py:7-28 (helper)Helper function to parse docstring for parameter descriptions used in schema generation for cmd_run inputSchema.def parse_docstring(docstring: str) -> dict[str, str]: """Parses a Google-style docstring to extract parameter descriptions.""" descriptions = {} if not docstring: return descriptions lines = docstring.split("\n") current_param = None for line in lines: line = line.strip() if line.startswith("Args:"): continue elif line and "(" in line and ")" in line and ":" in line: param = line.split("(")[0].strip() desc = line.split("):")[1].strip() descriptions[param] = desc current_param = param elif current_param and line: descriptions[current_param] += " " + line.strip() return descriptions