Call Tools in AI Workflows with Confluent Cloud

Streaming Agents can call external tools as part of AI workflows, enabling them to access additional data sources, perform complex computations, or interact with third-party services during agent execution. This capability enables agents to invoke custom functions dynamically or connect to external APIs and services, enhancing the flexibility and intelligence of your AI applications.

Tools can be based on user-defined functions (UDFs) or Model Context Protocol (MCP) servers.

You make a tool available to a streaming agent by using the CREATE TOOL statement to create a tool resource. An agent can access multiple tools, guided by the agent’s prompt.

How to call tools

These are the general steps for calling tools.

  • For a function-based tool, register a UDF by using the CREATE FUNCTION statement.
  • For an MCP-based tool:
    • Create a connection to an MCP server by using the CREATE CONNECTION statement.
    • Create a tool by using the CREATE TOOL statement and referencing the connection.
  • Create a remote model:
    • Create a connection to the model provider by using the CREATE CONNECTION statement.
    • Register a remote model by using the CREATE MODEL statement.
  • Create a streaming agent that calls the tools by using the CREATE AGENT statement.
  • Run the agent on streaming data by using the AI_RUN_AGENT function, which automates calls to the AI_TOOL_INVOKE function.

Call tools example

The following code example shows how to create a UDF-based tool and an MCP-based tool.

-- Register a UDF.
CREATE FUNCTION udf_function
USING JAR 'udf.jar'
COMMENT 'UDF-based tool';

-- Create a tool based on the UDF.
CREATE TOOL udf_tool
USING FUNCTION udf_function
WITH (
  'type' = 'function',
  'description' = 'UDF-based tool'
);

-- Create a connection to an MCP server.
CREATE CONNECTION mcp_connection
WITH (
  'type' = 'mcp_server',
  'endpoint' = 'https://mcp.example.com',
  'api-key' = '<your-api-key>',
  'transport-type' = 'STREAMABLE_HTTP'
);

-- Create a tool based on the connection.
CREATE TOOL mcp_tool
USING CONNECTION mcp_connection
WITH (
  'type' = 'mcp_server',
  'description' = 'MCP-based tool'
);

The following code example shows how to create a remote model.

-- Create a connection to the model provider.
CREATE CONNECTION openai_connection
WITH (
  'type' = 'openai',
  'endpoint' = 'https://api.openai.com/v1/chat/completions',
  'api-key' = 'your-openai-key'
);

-- Create a remote model.
CREATE MODEL my_model
INPUT (message STRING)
OUTPUT (response STRING)
WITH (
  'provider' = 'openai',
  'openai.connection' = 'openai_connection',
  'model' = 'gpt-4o',
);

The following code example shows how to create a streaming agent that uses the model and tools you created in the previous steps.

-- Create a streaming agent that uses the tools.
CREATE AGENT agent_with_tools
USING MODEL my_model
USING PROMPT 'You are a helpful agent that uses the tools to answer questions.'
USING TOOLS udf_tool, mcp_tool;

The following code example shows how to run the agent on streaming data by using the AI_RUN_AGENT function.

 -- Create a table for prompts.
 CREATE TABLE prompt_stream (
   id STRING,
   prompt STRING);

-- Insert a prompt into the table, with its ID.
INSERT INTO prompt_stream
  VALUES
    ('1', 'Fetch the content of https://www.amazon.com/primebigdealdays?ref_=nav_cs_td_pbdd_dt_cr and return price of any electronics mentioned in the page in Format {"Name": {"price", "discount"}} order by discount')

-- Run the agent on streaming data.
SELECT status, response FROM `prompt_stream`,
 LATERAL TABLE(AI_RUN_AGENT(`agent_with_tools`, `prompt`, `id`));

Streaming Agent examples

Find more examples in the following topics: