MCPcopy
hub / github.com/the-open-agent/openagent / QueryTextWithTools

Function QueryTextWithTools

model/mcp.go:164–257  ·  view source on GitHub ↗
(p ModelProvider, question string, writer io.Writer, history []*RawMessage, prompt string, knowledgeMessages []*RawMessage, toolSession *ToolSession, lang string)

Source from the content-addressed store, hash-verified

162}
163
164func QueryTextWithTools(p ModelProvider, question string, writer io.Writer, history []*RawMessage, prompt string, knowledgeMessages []*RawMessage, toolSession *ToolSession, lang string) (*ModelResult, error) {
165 var messages []*RawMessage
166
167 toolCount := 0
168 if toolSession.McpToolSet != nil {
169 toolCount = len(toolSession.McpToolSet.Tools)
170 if toolSession.McpToolSet.WebSearchEnabled {
171 toolCount++
172 }
173 }
174 fmt.Printf("\n--- LLM Call (Round 0) | Tools available: [%d] ---\n", toolCount)
175
176 modelResult, err := p.QueryText(question, writer, history, prompt, knowledgeMessages, toolSession, lang)
177 if err != nil {
178 return nil, err
179 }
180
181 toolCalls := normalizeToolCalls(toolSession)
182 if len(toolCalls) == 0 {
183 fmt.Printf("LLM Decision: [Final Answer — no tool calls]\n")
184 return modelResult, nil
185 }
186
187 round := 0
188 for len(toolCalls) > 0 {
189 round++
190 fmt.Printf("\n--- Agent Round %d | LLM Decision: [%d tool call(s)] ---\n", round, len(toolCalls))
191 for i, tc := range toolCalls {
192 fmt.Printf(" Tool %d: [%s] args: %s\n", i+1, tc.Function.Name, tc.Function.Arguments)
193 }
194
195 roundHasToolError := false
196 var roundImages []ImageAttachment
197 for _, toolCall := range toolCalls {
198 serverName, toolName := mcp.GetServerNameAndToolNameFromId(toolCall.Function.Name)
199
200 messages = append(messages, &RawMessage{
201 Text: "",
202 Author: "AI",
203 ReasoningContent: toolSession.ToolMessages.ReasoningContent,
204 ToolCall: toolCall,
205 })
206
207 var toolFailed bool
208 var images []ImageAttachment
209 messages, images, toolFailed, err = callMcpTool(toolCall, serverName, toolName, toolSession.IsVision, toolSession.McpToolSet, messages, writer, lang)
210 if err != nil {
211 return nil, err
212 }
213 roundImages = append(roundImages, images...)
214 if toolFailed {
215 roundHasToolError = true
216 }
217 }
218 if len(roundImages) > 0 {
219 messages = append(messages, &RawMessage{
220 Text: "Images returned by image_search, in the same order as the result metadata.",
221 Author: "User",

Callers 3

generateMessageAnswerFunction · 0.92
GetAnswerWithToolFunction · 0.92

Calls 5

normalizeToolCallsFunction · 0.85
callMcpToolFunction · 0.85
QueryTextMethod · 0.65
CloseMethod · 0.45

Tested by

no test coverage detected