Skip to content

Commit 0ab2706

Browse files
authored
Merge pull request #129 from koic/add_example_of_define_tool_and_define_protocol_to_read
[Doc] Add example of `define_tool` and `define_protocol`
2 parents 97640ed + e5bb17e commit 0ab2706

File tree

1 file changed

+49
-2
lines changed

1 file changed

+49
-2
lines changed

README.md

Lines changed: 49 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -371,7 +371,7 @@ If no exception reporter is configured, a default no-op reporter is used that si
371371

372372
MCP spec includes [Tools](https://modelcontextprotocol.io/specification/2025-06-18/server/tools) which provide functionality to LLM apps.
373373

374-
This gem provides a `MCP::Tool` class that can be used to create tools in two ways:
374+
This gem provides a `MCP::Tool` class that can be used to create tools in three ways:
375375

376376
1. As a class definition:
377377

@@ -417,6 +417,22 @@ tool = MCP::Tool.define(
417417
end
418418
```
419419

420+
3. By using the `ModelContextProtocol::Server#define_tool` method with a block:
421+
422+
```ruby
423+
server = ModelContextProtocol::Server.new
424+
server.define_tool(
425+
name: "my_tool",
426+
description: "This tool performs specific functionality...",
427+
annotations: {
428+
title: "My Tool",
429+
read_only_hint: true
430+
}
431+
) do |args, server_context|
432+
Tool::Response.new([{ type: "text", text: "OK" }])
433+
end
434+
```
435+
420436
The server_context parameter is the server_context passed into the server and can be used to pass per request information,
421437
e.g. around authentication state.
422438

@@ -436,7 +452,7 @@ Annotations can be set either through the class definition using the `annotation
436452

437453
MCP spec includes [Prompts](https://modelcontextprotocol.io/specification/2025-06-18/server/prompts), which enable servers to define reusable prompt templates and workflows that clients can easily surface to users and LLMs.
438454

439-
The `MCP::Prompt` class provides two ways to create prompts:
455+
The `MCP::Prompt` class provides three ways to create prompts:
440456

441457
1. As a class definition with metadata:
442458

@@ -506,6 +522,37 @@ prompt = MCP::Prompt.define(
506522
end
507523
```
508524

525+
3. Using the `ModelContextProtocol::Server#define_protocol` method:
526+
527+
```ruby
528+
server = ModelContextProtocol::Server.new
529+
server.define_protocol(
530+
name: "my_prompt",
531+
description: "This prompt performs specific functionality...",
532+
arguments: [
533+
Prompt::Argument.new(
534+
name: "message",
535+
description: "Input message",
536+
required: true
537+
)
538+
]
539+
) do |args, server_context:|
540+
Prompt::Result.new(
541+
description: "Response description",
542+
messages: [
543+
Prompt::Message.new(
544+
role: "user",
545+
content: Content::Text.new("User message")
546+
),
547+
Prompt::Message.new(
548+
role: "assistant",
549+
content: Content::Text.new(args["message"])
550+
)
551+
]
552+
)
553+
end
554+
```
555+
509556
The server_context parameter is the server_context passed into the server and can be used to pass per request information,
510557
e.g. around authentication state or user preferences.
511558

0 commit comments

Comments
 (0)