embabel-agent icon indicating copy to clipboard operation
embabel-agent copied to clipboard

[Feature Request] Support User-Controlled Tool Execution as Spring AI

Open zhuyouxiang opened this issue 10 months ago • 13 comments

While PromptRunner is the standard interface for LLM calls, scenarios like fast research only require the LLM for query understanding and parameter decomposition.

In these cases, the final "Generation" step is redundant. Moreover, the "Long-In, Long-Out" nature of these tasks causes significant latency.

Could Embabel support User-Controlled Tool Execution, similar to Spring AI: User-Controlled Tool Execution? This would allow PromptRunner to terminate immediately after tool execution, bypassing the final generation round to minimize latency and cost while preserving the Agent's intent-parsing capabilities.

zhuyouxiang avatar Jan 30 '26 06:01 zhuyouxiang

Interesting use case.

Since we took full control of the agentic loop, an Embabel tool can trigger replanning. See Tool.replanAlways and related methods.

If this doesn't meet your need, we could also have a tool trigger loop exit. This would be easy to implement, but we'd have to decide how the user handles the return of the PromptRunner call. An exception?

johnsonr avatar Jan 30 '26 10:01 johnsonr

Thanks for the suggestion!

I've explored the Tool.replanAlways approach, but I found a limitation in the current implementation that prevents the application layer from retrieving the tool's execution result.

By the way, Is there a more concise way to retrieve the MCP Tool instance from OperationContext?

	@Action
	public String fastSearch(UserInput userInput, OperationContext context) {
		Tool webSearch = context
				.agentPlatform()
				.getToolGroupResolver()
				.resolveToolGroup(new ToolGroupRequirement(CoreToolGroups.WEB))
				.getResolvedToolGroup()
				.getTools()
				.getFirst();

		try {
			context.ai().withAutoLlm().withTool(Tool.replanAlways(webSearch)).generateText("");
		} catch (ReplanRequestedException e) {
			e.getBlackboardUpdater().accept(context.getAgentProcess().getBlackboard());
		}

		return "";
	}

When updating the blackboard, the tool's result isn't being properly mapped or preserved for the final output. Specifically:

fun replanAlways(tool: Tool): Tool {
    return ConditionalReplanningTool(tool) { context ->
        ReplanDecision("${tool.definition.name} replans") { bb ->
            context.artifact?.let { bb.addObject(it) }
        }
    }
}

Something else I noticed, but I'm not sure if it's a concern. I initially used createObject instead of generateText,buildResult will exception when call outputParser

DefaultToolLoop
            for (toolCall in assistantMessage.toolCalls) {
                val shouldContinue = processToolCall(toolCall, state)
                if (!shouldContinue) {
                    // Tool requested replan - terminate loop gracefully
                    logger.info("Tool loop terminated for replan after {} iterations", state.iterations)
                    return buildResult("", outputParser, state)
                }
            }

zhuyouxiang avatar Jan 31 '26 03:01 zhuyouxiang

If this doesn't meet your need, we could also have a tool trigger loop exit. This would be easy to implement, but we'd have to decide how the user handles the return of the PromptRunner call. An exception?

Tool.replanAlways is not quite ideal, As it is per-tool, but since the LLM can trigger concurrent calls, we can't capture the complete results. Return type handling is indeed a major challenge, perhaps we should expose Flux<ChatResponse> and append tool result to Message list?

zhuyouxiang avatar Jan 31 '26 03:01 zhuyouxiang

There is now extensive support for taking artifacts from tools. See https://docs.embabel.com/embabel-agent/guide/0.3.4-SNAPSHOT/#artifactsinkingtool-capturing-tool-outputs

johnsonr avatar Feb 01 '26 23:02 johnsonr

Re MCP tools, see https://github.com/embabel/impromptu/blob/main/src/main/java/com/embabel/impromptu/agents/ChatConfiguration.java for the best way to expose MCP tools in an isolated way.

johnsonr avatar Feb 01 '26 23:02 johnsonr

Given this discussion, is there still a concrete enhancement that would meet your need? Thanks

johnsonr avatar Feb 03 '26 21:02 johnsonr

Given this discussion, is there still a concrete enhancement that would meet your need? Thanks

  1. ReplanningTool meets my need, which handles Text (content) Result. This issue can be closed.
ReplanningTool searchOnly = new ReplanningTool(webSearch, "search only");
  1. The handling of Result is inconsistent between ReplanningTool(content only) and Tool.replanAlways (using ConditionalReplanningTool? artifact only ), this needs to be reviewed.
Image Image

zhuyouxiang avatar Feb 04 '26 06:02 zhuyouxiang

@alexheifetz review if this is actually fixed.

alexheifetz avatar Mar 25 '26 20:03 alexheifetz

There appears to be an inconsistency in how ReplanningTool and Tool.replanAlways handle Tool.Result.WithArtifact — ReplanningTool silently drops the artifact, while Tool.replanAlways drops the text content for Tool.Result.Text results.

@johnsonr please advice if this warrants a fix or it's a separate issue.

alexheifetz avatar Apr 10 '26 15:04 alexheifetz

@zhuyouxiang - thanks for your patience. There were recent enhancements to controlling tool loop execution, that you might find meeting you requirements as well, specifically - tool callbacks/interceptors:

https://docs.embabel.com/embabel-agent/guide/0.4.0/index.html#reference.interceptors

Thank you

igordayen avatar Jun 23 '26 15:06 igordayen

This would allow PromptRunner to terminate immediately after tool execution, bypassing the final generation round to minimize latency and cost while preserving the Agent's intent-parsing capabilities.

https://docs.embabel.com/embabel-agent/guide/0.4.0/index.html#reference.termination

@zhuyouxiang - could you please update/confirm? thanks

igordayen avatar Jun 23 '26 23:06 igordayen

@zhuyouxiang - Hello, soliciting feedback before closing the ticket, thank you.

igordayen avatar Jun 25 '26 13:06 igordayen

@zhuyouxiang - closing; feel free to reopen as needed. Thank you.

igordayen avatar Jun 26 '26 21:06 igordayen