Skip to content

Commit e5bb17e

Browse files
committed
[Doc] Add example of define_tool and define_protocol
This PR adds example of `define_tool` and `define_protocol`.
1 parent eb0d9c0 commit e5bb17e

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
@@ -369,7 +369,7 @@ If no exception reporter is configured, a default no-op reporter is used that si
369369

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

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

374374
1. As a class definition:
375375

@@ -415,6 +415,22 @@ tool = MCP::Tool.define(
415415
end
416416
```
417417

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

@@ -434,7 +450,7 @@ Annotations can be set either through the class definition using the `annotation
434450

435451
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.
436452

437-
The `MCP::Prompt` class provides two ways to create prompts:
453+
The `MCP::Prompt` class provides three ways to create prompts:
438454

439455
1. As a class definition with metadata:
440456

@@ -504,6 +520,37 @@ prompt = MCP::Prompt.define(
504520
end
505521
```
506522

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

0 commit comments

Comments
 (0)