跳到主要内容
让代理访问 shell 功能强大(尽管在沙盒环境之外存在风险)。 LLM 可以使用它执行任何 shell 命令。常见的用例是让 LLM 与您的本地文件系统进行交互。 注意: Shell 工具不适用于 Windows 操作系统。
pip install -qU langchain-community
from langchain_community.tools import ShellTool

shell_tool = ShellTool()
print(shell_tool.run({"commands": ["echo 'Hello World!'", "time"]}))
Hello World!

real 0m0.000s
user 0m0.000s
sys 0m0.000s
/Users/wfh/code/lc/lckg/langchain/tools/shell/tool.py:34: UserWarning: The shell tool has no safeguards by default. Use at your own risk.
  warnings.warn(

与代理一起使用

与所有工具一样,这些工具可以交给代理以完成更复杂的任务。让我们让代理从网页中获取一些链接。
from langchain.agents import create_agent


tools = [shell_tool]
agent = create_agent("gpt-4.1-mini", tools)

input_message = {
    "role": "user",
    "content": (
        "Download the README here and identify the link for LangChain tutorials: "
        "https://raw.githubusercontent.com/langchain-ai/langchain/master/README.md"
    ),
}

for step in agent.stream(
    {"messages": [input_message]},
        stream_mode="values",
):
    step["messages"][-1].pretty_print()
================================ Human Message =================================

Download the README here and identify the link for LangChain tutorials: https://raw.githubusercontent.com/langchain-ai/langchain/master/README.md
================================== Ai Message ==================================
Tool Calls:
  terminal (call_mr86V0d6E9nQiJZT7Xw5fH0G)
 Call ID: call_mr86V0d6E9nQiJZT7Xw5fH0G
  Args:
    commands: ['curl -o README.md https://raw.githubusercontent.com/langchain-ai/langchain/master/README.md']
Executing command:
 ['curl -o README.md https://raw.githubusercontent.com/langchain-ai/langchain/master/README.md']
================================= Tool Message =================================
Name: terminal

  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
100  5169  100  5169    0     0   114k      0 --:--:-- --:--:-- --:--:--  114k
/langchain/libs/community/langchain_community/tools/shell/tool.py:33: UserWarning: The shell tool has no safeguards by default. Use at your own risk.
  warnings.warn(
================================== Ai Message ==================================
Tool Calls:
  terminal (call_LF8TGrgS84WvUvaazYnVfib8)
 Call ID: call_LF8TGrgS84WvUvaazYnVfib8
  Args:
    commands: ["grep -i 'tutorial' README.md"]
Executing command:
 ["grep -i 'tutorial' README.md"]
================================= Tool Message =================================
Name: terminal

- [Tutorials](https://python.langchain.ac.cn/docs/tutorials/): Simple walkthroughs with
/langchain/libs/community/langchain_community/tools/shell/tool.py:33: UserWarning: The shell tool has no safeguards by default. Use at your own risk.
  warnings.warn(
================================== Ai Message ==================================

The link for LangChain tutorials in the README is: https://python.langchain.ac.cn/docs/tutorials/

以编程方式连接这些文档到 Claude、VSCode 等,通过 MCP 获取实时答案。
© . This site is unofficial and not affiliated with LangChain, Inc.