|
3 | 3 | require 'test_helper'
|
4 | 4 | require 'stringio'
|
5 | 5 |
|
6 |
| -class TestTinyMCP < Minitest::Test |
| 6 | +class TinyMCPTest < Minitest::Test |
7 | 7 | def test_that_it_has_a_version_number
|
8 | 8 | refute_nil ::TinyMCP::VERSION
|
9 | 9 | end
|
@@ -274,7 +274,7 @@ def test_tools_call_nonexistent_tool
|
274 | 274 |
|
275 | 275 | assert response[:error]
|
276 | 276 | assert_equal(-32602, response[:error][:code])
|
277 |
| - assert_match(/Unknown_tool: nonexistent/, response[:error][:message]) |
| 277 | + assert_match(/Unknown tool: nonexistent/, response[:error][:message]) |
278 | 278 | end
|
279 | 279 |
|
280 | 280 | def test_tools_call_with_error
|
@@ -337,7 +337,8 @@ def test_error_types
|
337 | 337 |
|
338 | 338 | def test_error_with_custom_message
|
339 | 339 | server = TinyMCP::Server.new
|
340 |
| - error = server.send(:error_for, { 'id' => 1 }, :internal, 'Custom error message') |
| 340 | + error = |
| 341 | + server.send(:error_for, { 'id' => 1 }, :internal, 'Custom error message') |
341 | 342 |
|
342 | 343 | assert_equal(-32603, error[:error][:code])
|
343 | 344 | assert_equal 'Custom error message', error[:error][:message]
|
@@ -501,4 +502,169 @@ def call(test_param:)
|
501 | 502 | response = server.send(:handle_request, request)
|
502 | 503 | assert_equal 'Received: value', response[:result][:content][0][:text]
|
503 | 504 | end
|
| 505 | + |
| 506 | + # Multi-modal Content Tests |
| 507 | + def test_tool_returning_array_of_content_items |
| 508 | + multi_content_tool_class = Class.new(TinyMCP::Tool) do |
| 509 | + name 'multi_content' |
| 510 | + desc 'Returns multiple content items' |
| 511 | + arg :content_type, :string, 'Type of content to return' |
| 512 | + |
| 513 | + def call(content_type:) |
| 514 | + case content_type |
| 515 | + when 'multiple_text' |
| 516 | + [ |
| 517 | + { type: 'text', text: 'First text item' }, |
| 518 | + { type: 'text', text: 'Second text item' }, |
| 519 | + { type: 'text', text: 'Third text item' } |
| 520 | + ] |
| 521 | + when 'mixed' |
| 522 | + [ |
| 523 | + { type: 'text', text: 'Some text content' }, |
| 524 | + { type: 'image', |
| 525 | + data: 'base64-encoded-image-data', |
| 526 | + mimeType: 'image/png' }, |
| 527 | + { type: 'text', text: 'More text after image' } |
| 528 | + ] |
| 529 | + end |
| 530 | + end |
| 531 | + end |
| 532 | + |
| 533 | + server = TinyMCP::Server.new(multi_content_tool_class) |
| 534 | + |
| 535 | + # Test multiple text content |
| 536 | + request = { |
| 537 | + 'jsonrpc' => '2.0', |
| 538 | + 'id' => 1, |
| 539 | + 'method' => 'tools/call', |
| 540 | + 'params' => { |
| 541 | + 'name' => 'multi_content', |
| 542 | + 'arguments' => { 'content_type' => 'multiple_text' } |
| 543 | + } |
| 544 | + } |
| 545 | + |
| 546 | + response = server.send(:handle_request, request) |
| 547 | + content = response[:result][:content] |
| 548 | + |
| 549 | + assert_equal 3, content.length |
| 550 | + assert_equal 'First text item', content[0][:text] |
| 551 | + assert_equal 'Second text item', content[1][:text] |
| 552 | + assert_equal 'Third text item', content[2][:text] |
| 553 | + content.each { |item| assert_equal 'text', item[:type] } |
| 554 | + end |
| 555 | + |
| 556 | + def test_tool_returning_mixed_content_types |
| 557 | + mixed_tool_class = Class.new(TinyMCP::Tool) do |
| 558 | + name 'mixed_content' |
| 559 | + desc 'Returns mixed content types' |
| 560 | + |
| 561 | + def call |
| 562 | + [ |
| 563 | + { type: 'text', text: 'Here is some text' }, |
| 564 | + { type: 'image', |
| 565 | + mimeType: 'image/png', |
| 566 | + data: 'iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAADUlEQVR42' \ |
| 567 | + 'mP8/5+hHgAHggJ/PchI7wAAAABJRU5ErkJggg==' }, |
| 568 | + { type: 'text', text: 'And more text after the image' }, |
| 569 | + { type: 'resource', |
| 570 | + uri: 'file:///path/to/resource.txt', |
| 571 | + text: 'Resource reference' } |
| 572 | + ] |
| 573 | + end |
| 574 | + end |
| 575 | + |
| 576 | + server = TinyMCP::Server.new(mixed_tool_class) |
| 577 | + request = { |
| 578 | + 'jsonrpc' => '2.0', |
| 579 | + 'id' => 1, |
| 580 | + 'method' => 'tools/call', |
| 581 | + 'params' => { |
| 582 | + 'name' => 'mixed_content', |
| 583 | + 'arguments' => {} |
| 584 | + } |
| 585 | + } |
| 586 | + |
| 587 | + response = server.send(:handle_request, request) |
| 588 | + content = response[:result][:content] |
| 589 | + |
| 590 | + assert_equal 4, content.length |
| 591 | + |
| 592 | + # Check text content |
| 593 | + assert_equal 'text', content[0][:type] |
| 594 | + assert_equal 'Here is some text', content[0][:text] |
| 595 | + |
| 596 | + # Check image content |
| 597 | + assert_equal 'image', content[1][:type] |
| 598 | + assert_equal 'image/png', content[1][:mimeType] |
| 599 | + assert_equal 'iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAADUlEQVR42' \ |
| 600 | + 'mP8/5+hHgAHggJ/PchI7wAAAABJRU5ErkJggg==', |
| 601 | + content[1][:data] |
| 602 | + |
| 603 | + # Check second text content |
| 604 | + assert_equal 'text', content[2][:type] |
| 605 | + assert_equal 'And more text after the image', content[2][:text] |
| 606 | + |
| 607 | + # Check resource content |
| 608 | + assert_equal 'resource', content[3][:type] |
| 609 | + assert_equal 'file:///path/to/resource.txt', content[3][:uri] |
| 610 | + assert_equal 'Resource reference', content[3][:text] |
| 611 | + end |
| 612 | + |
| 613 | + def test_tool_returning_empty_array |
| 614 | + empty_tool_class = Class.new(TinyMCP::Tool) do |
| 615 | + name 'empty_content' |
| 616 | + desc 'Returns empty content array' |
| 617 | + |
| 618 | + def call |
| 619 | + [] |
| 620 | + end |
| 621 | + end |
| 622 | + |
| 623 | + server = TinyMCP::Server.new(empty_tool_class) |
| 624 | + request = { |
| 625 | + 'jsonrpc' => '2.0', |
| 626 | + 'id' => 1, |
| 627 | + 'method' => 'tools/call', |
| 628 | + 'params' => { |
| 629 | + 'name' => 'empty_content', |
| 630 | + 'arguments' => {} |
| 631 | + } |
| 632 | + } |
| 633 | + |
| 634 | + response = server.send(:handle_request, request) |
| 635 | + content = response[:result][:content] |
| 636 | + |
| 637 | + assert_equal [], content |
| 638 | + assert_equal 0, content.length |
| 639 | + end |
| 640 | + |
| 641 | + def test_tool_returning_single_item_array |
| 642 | + single_tool_class = Class.new(TinyMCP::Tool) do |
| 643 | + name 'single_content' |
| 644 | + desc 'Returns single content item in array' |
| 645 | + arg :message, :string, 'Message to return' |
| 646 | + |
| 647 | + def call(message:) |
| 648 | + [{ type: 'text', text: message }] |
| 649 | + end |
| 650 | + end |
| 651 | + |
| 652 | + server = TinyMCP::Server.new(single_tool_class) |
| 653 | + request = { |
| 654 | + 'jsonrpc' => '2.0', |
| 655 | + 'id' => 1, |
| 656 | + 'method' => 'tools/call', |
| 657 | + 'params' => { |
| 658 | + 'name' => 'single_content', |
| 659 | + 'arguments' => { 'message' => 'Single item message' } |
| 660 | + } |
| 661 | + } |
| 662 | + |
| 663 | + response = server.send(:handle_request, request) |
| 664 | + content = response[:result][:content] |
| 665 | + |
| 666 | + assert_equal 1, content.length |
| 667 | + assert_equal 'text', content[0][:type] |
| 668 | + assert_equal 'Single item message', content[0][:text] |
| 669 | + end |
504 | 670 | end
|
0 commit comments