|
59 | 59 |
|
60 | 60 | MCP spec includes [Tools](https://modelcontextprotocol.io/docs/concepts/tools) which provide functionality to LLM apps. |
61 | 61 |
|
62 | | -This gem provides a `ModelContextProtocol::Tool` class that can be instantiated to create tools. |
| 62 | +This gem provides a `ModelContextProtocol::Tool` class that can be used to create tools in two ways: |
63 | 63 |
|
64 | | -Tools can be passed into the `ModelContextProtocol::Server` constructor to register them with the server. |
| 64 | +1. As a class definition: |
| 65 | +```ruby |
| 66 | +class MyTool < ModelContextProtocol::Tool |
| 67 | + tool_name "my_tool" |
| 68 | + tool_description "This tool performs specific functionality..." |
| 69 | + tool_input_schema [{ type: "text", name: "message" }] |
65 | 70 |
|
66 | | -### Example Tool Implementation |
| 71 | + def call(message) |
| 72 | + Tool::Response.new([{ type: "text", content: "OK" }]) |
| 73 | + end |
| 74 | +end |
67 | 75 |
|
68 | | -The `Tool` class allows creating tools that can be used within the model context. |
69 | | -To create a tool, instantiate the class with the required parameters and an optional block: |
| 76 | +tool = MyTool.new |
| 77 | +``` |
70 | 78 |
|
| 79 | +2. By using the `ModelContextProtocol::Tool.define` method with a block: |
71 | 80 | ```ruby |
72 | | -tool = ModelContextProtocol::Tool.new(name: "my_tool", description: "This tool performs specific functionality...") do |args| |
73 | | - # Implement the tool's functionality here |
74 | | - result = process_something(args["parameter_name"]) |
75 | | - ModelContextProtocol::Tool::Response.new([{ type: "text", text: result }], false ) |
| 81 | +tool = ModelContextProtocol::Tool.define(name: "my_tool", description: "This tool performs specific functionality...") do |args| |
| 82 | + Tool::Response.new([{ type: "text", content: "OK" }]) |
76 | 83 | end |
77 | 84 | ``` |
78 | 85 |
|
79 | 86 | ## Prompts |
80 | 87 |
|
81 | | -MCP spec includes |
82 | | -[Prompts](https://modelcontextprotocol.io/docs/concepts/prompts), `Prompts` enable servers to define reusable prompt |
83 | | -templates and workflows that clients can easily surface to users and LLMs |
| 88 | +MCP spec includes [Prompts](https://modelcontextprotocol.io/docs/concepts/prompts), which enable servers to define reusable prompt templates and workflows that clients can easily surface to users and LLMs. |
84 | 89 |
|
85 | | -The `Prompt` class allows creating prompts that can be used within the model context. |
| 90 | +The `ModelContextProtocol::Prompt` class provides two ways to create prompts: |
86 | 91 |
|
87 | | -To create a prompt, instantiate the class with the required parameters and an optional block: |
| 92 | +1. As a class definition with metadata: |
| 93 | +```ruby |
| 94 | +class MyPrompt < ModelContextProtocol::Prompt |
| 95 | + prompt_name "my_prompt" # Optional - defaults to underscored class name |
| 96 | + description "This prompt performs specific functionality..." |
| 97 | + arguments [ |
| 98 | + Prompt::Argument.new( |
| 99 | + name: "message", |
| 100 | + description: "Input message", |
| 101 | + required: true |
| 102 | + ) |
| 103 | + ] |
| 104 | + |
| 105 | + def template(args) |
| 106 | + Prompt::Result.new( |
| 107 | + description: "Response description", |
| 108 | + messages: [ |
| 109 | + Prompt::Message.new( |
| 110 | + role: "user", |
| 111 | + content: Content::Text.new("User message") |
| 112 | + ), |
| 113 | + Prompt::Message.new( |
| 114 | + role: "assistant", |
| 115 | + content: Content::Text.new(args["message"]) |
| 116 | + ) |
| 117 | + ] |
| 118 | + ) |
| 119 | + end |
| 120 | +end |
| 121 | +``` |
88 | 122 |
|
| 123 | +2. Using the `ModelContextProtocol::Prompt.define` method: |
89 | 124 | ```ruby |
90 | | -prompt = ModelContextProtocol::Prompt.new(name: "my_prompt", description: "This prompt performs specific functionality...") do |args| |
91 | | - # Implement the prompt's functionality here |
92 | | - result = template_something(args["parameter_name"]) |
93 | | - ModelContextProtocol::Prompt::Response.new([{ type: "text", text: result }], false ) |
| 125 | +prompt = ModelContextProtocol::Prompt.define( |
| 126 | + name: "my_prompt", |
| 127 | + description: "This prompt performs specific functionality...", |
| 128 | + arguments: [ |
| 129 | + Prompt::Argument.new( |
| 130 | + name: "message", |
| 131 | + description: "Input message", |
| 132 | + required: true |
| 133 | + ) |
| 134 | + ] |
| 135 | +) do |args| |
| 136 | + Prompt::Result.new( |
| 137 | + description: "Response description", |
| 138 | + messages: [ |
| 139 | + Prompt::Message.new( |
| 140 | + role: "user", |
| 141 | + content: Content::Text.new("User message") |
| 142 | + ), |
| 143 | + Prompt::Message.new( |
| 144 | + role: "assistant", |
| 145 | + content: Content::Text.new(args["message"]) |
| 146 | + ) |
| 147 | + ] |
| 148 | + ) |
94 | 149 | end |
95 | 150 | ``` |
96 | 151 |
|
| 152 | +### Key Components |
| 153 | + |
| 154 | +- `Prompt::Argument` - Defines input parameters for the prompt template |
| 155 | +- `Prompt::Message` - Represents a message in the conversation with a role and content |
| 156 | +- `Prompt::Result` - The output of a prompt template containing description and messages |
| 157 | +- `Content::Text` - Text content for messages |
| 158 | + |
| 159 | +### Usage |
| 160 | + |
| 161 | +Register prompts with the MCP server: |
97 | 162 |
|
| 163 | +```ruby |
| 164 | +server = ModelContextProtocol::Server.new( |
| 165 | + name: "my_server", |
| 166 | + prompts: [MyPrompt.new] |
| 167 | +) |
| 168 | +``` |
98 | 169 |
|
| 170 | +The server will handle prompt listing and execution through the MCP protocol methods: |
| 171 | +- `prompts/list` - Lists all registered prompts and their schemas |
| 172 | +- `prompts/get` - Retrieves and executes a specific prompt with arguments |
99 | 173 |
|
100 | 174 | ## Releases |
101 | 175 |
|
|
0 commit comments