[dify] function calling

manifest.yaml

name: "agent_strategy_demo"
description: "Sample Agent Strategy plugin with Function Calling"
version: "1.0.0"

tools:
- name: "calculator"
description: "Perform basic math operations"
parameters:
type: object
properties:
expression:
type: string
description: "Math expression to evaluate"
required: ["expression"]

agent_strategy:
entry: agent_strategy.py:SampleAgentStrategy

agent_strategy.py

from dify_plugin import AgentStrategy

class SampleAgentStrategy(AgentStrategy):
def run(self, llm_response, tools):
"""
llm_response: dict (may contain tool call request)
tools: dict of tools defined in manifest
"""
# If model requests a tool execution
if "tool" in llm_response:
tool_name = llm_response["tool"]["name"]
tool_args = llm_response["tool"]["arguments"]

tool = tools.get(tool_name)
if tool is None:
return {
"type": "message",
"content": f"Tool '{tool_name}' not found."
}

result = tool.run(**tool_args)
return {
"type": "tool_result",
"tool_name": tool_name,
"result": result
}

# Regular text response
return {
"type": "message",
"content": llm_response.get("content", "")
}

calclator.py

from dify_plugin import Tool

class CalculatorTool(Tool):
def run(self, expression: str):
try:
result = eval(expression)
return {"result": result}
except Exception as e:
return {"error": str(e)}