Skip to content

Commit b694571

Browse files
Use ellmer:::tool_string() to format tool results (#78)
Co-authored-by: simonpcouch <[email protected]>
1 parent 2105c5c commit b694571

File tree

3 files changed

+7
-4
lines changed

3 files changed

+7
-4
lines changed

NEWS.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
# mcptools (development version)
22

3+
* `mcp_server()` now formats tool results in the same way as ellmer (#78 by @gadenbuie).
34
* `mcp_tools()` now sends and receives a `"notifications/initialized"` (#77 by @galachad).
45
* `mcp_session()` now returns invisibly the nanonext socket used for communicating with the server.
56
* `mcp_server()` gains logical argument `session_tools`, allowing users to opt-out of presenting R sessions tools to clients.

R/session.R

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -55,18 +55,20 @@ handle_message_from_server <- function(data) {
5555

5656
as_tool_call_result <- function(data, result) {
5757
is_error <- FALSE
58+
format_result <- function(x) paste(x, collapse = "\n")
59+
5860
if (inherits(result, "ellmer::ContentToolResult")) {
5961
is_error <- !is.null(result@error)
60-
result <- result@value %||% result@error
62+
format_result <- asNamespace("ellmer")[["tool_string"]] %||% format_result
6163
}
62-
64+
6365
jsonrpc_response(
6466
data$id,
6567
list(
6668
content = list(
6769
list(
6870
type = "text",
69-
text = paste(result, collapse = "\n")
71+
text = format_result(result)
7072
)
7173
),
7274
isError = is_error

tests/testthat/test-session.R

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ test_that("as_tool_call_result handles ContentToolResult with error", {
4141

4242
output <- as_tool_call_result(data, tool_result)
4343

44-
expect_equal(output$result$content[[1]]$text, "error message")
44+
expect_match(output$result$content[[1]]$text, "error message")
4545
expect_true(output$result$isError)
4646
})
4747

0 commit comments

Comments
 (0)