Merge pull request #87 from koic/add_instructions_support_to_mcp_server
Add `instructions` support to `MCP::Server`
This commit is contained in:
commit
930173a04e
3 changed files with 41 additions and 3 deletions
|
|
@ -155,11 +155,11 @@ You can use the `Server#handle_json` method to handle requests.
|
||||||
|
|
||||||
```ruby
|
```ruby
|
||||||
class ApplicationController < ActionController::Base
|
class ApplicationController < ActionController::Base
|
||||||
|
|
||||||
def index
|
def index
|
||||||
server = MCP::Server.new(
|
server = MCP::Server.new(
|
||||||
name: "my_server",
|
name: "my_server",
|
||||||
version: "1.0.0",
|
version: "1.0.0",
|
||||||
|
instructions: "Use the tools of this server as a last resort",
|
||||||
tools: [SomeTool, AnotherTool],
|
tools: [SomeTool, AnotherTool],
|
||||||
prompts: [MyPrompt],
|
prompts: [MyPrompt],
|
||||||
server_context: { user_id: current_user.id },
|
server_context: { user_id: current_user.id },
|
||||||
|
|
|
||||||
|
|
@ -31,11 +31,12 @@ module MCP
|
||||||
|
|
||||||
include Instrumentation
|
include Instrumentation
|
||||||
|
|
||||||
attr_accessor :name, :version, :tools, :prompts, :resources, :server_context, :configuration, :capabilities, :transport
|
attr_accessor :name, :version, :instructions, :tools, :prompts, :resources, :server_context, :configuration, :capabilities, :transport
|
||||||
|
|
||||||
def initialize(
|
def initialize(
|
||||||
name: "model_context_protocol",
|
name: "model_context_protocol",
|
||||||
version: DEFAULT_VERSION,
|
version: DEFAULT_VERSION,
|
||||||
|
instructions: nil,
|
||||||
tools: [],
|
tools: [],
|
||||||
prompts: [],
|
prompts: [],
|
||||||
resources: [],
|
resources: [],
|
||||||
|
|
@ -47,6 +48,7 @@ module MCP
|
||||||
)
|
)
|
||||||
@name = name
|
@name = name
|
||||||
@version = version
|
@version = version
|
||||||
|
@instructions = instructions
|
||||||
@tools = tools.to_h { |t| [t.name_value, t] }
|
@tools = tools.to_h { |t| [t.name_value, t] }
|
||||||
@prompts = prompts.to_h { |p| [p.name_value, p] }
|
@prompts = prompts.to_h { |p| [p.name_value, p] }
|
||||||
@resources = resources
|
@resources = resources
|
||||||
|
|
@ -54,6 +56,12 @@ module MCP
|
||||||
@resource_index = index_resources_by_uri(resources)
|
@resource_index = index_resources_by_uri(resources)
|
||||||
@server_context = server_context
|
@server_context = server_context
|
||||||
@configuration = MCP.configuration.merge(configuration)
|
@configuration = MCP.configuration.merge(configuration)
|
||||||
|
|
||||||
|
if @configuration.protocol_version == "2024-11-05" && @instructions
|
||||||
|
message = "`instructions` supported by protocol version 2025-03-26 or higher"
|
||||||
|
raise ArgumentError, message
|
||||||
|
end
|
||||||
|
|
||||||
@capabilities = capabilities || default_capabilities
|
@capabilities = capabilities || default_capabilities
|
||||||
|
|
||||||
@handlers = {
|
@handlers = {
|
||||||
|
|
@ -218,7 +226,8 @@ module MCP
|
||||||
protocolVersion: configuration.protocol_version,
|
protocolVersion: configuration.protocol_version,
|
||||||
capabilities: capabilities,
|
capabilities: capabilities,
|
||||||
serverInfo: server_info,
|
serverInfo: server_info,
|
||||||
}
|
instructions: instructions,
|
||||||
|
}.compact
|
||||||
end
|
end
|
||||||
|
|
||||||
def list_tools(request)
|
def list_tools(request)
|
||||||
|
|
|
||||||
|
|
@ -62,6 +62,7 @@ module MCP
|
||||||
@server = Server.new(
|
@server = Server.new(
|
||||||
name: @server_name,
|
name: @server_name,
|
||||||
version: "1.2.3",
|
version: "1.2.3",
|
||||||
|
instructions: "Optional instructions for the client",
|
||||||
tools: [@tool, @tool_that_raises],
|
tools: [@tool, @tool_that_raises],
|
||||||
prompts: [@prompt],
|
prompts: [@prompt],
|
||||||
resources: [@resource],
|
resources: [@resource],
|
||||||
|
|
@ -133,6 +134,7 @@ module MCP
|
||||||
name: @server_name,
|
name: @server_name,
|
||||||
version: "1.2.3",
|
version: "1.2.3",
|
||||||
},
|
},
|
||||||
|
instructions: "Optional instructions for the client",
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -748,6 +750,33 @@ module MCP
|
||||||
assert_equal Server::DEFAULT_VERSION, response[:result][:serverInfo][:version]
|
assert_equal Server::DEFAULT_VERSION, response[:result][:serverInfo][:version]
|
||||||
end
|
end
|
||||||
|
|
||||||
|
test "server uses instructions when not configured" do
|
||||||
|
server = Server.new(name: "test_server")
|
||||||
|
request = {
|
||||||
|
jsonrpc: "2.0",
|
||||||
|
method: "initialize",
|
||||||
|
id: 1,
|
||||||
|
}
|
||||||
|
|
||||||
|
response = server.handle(request)
|
||||||
|
refute response[:result].key?(:instructions)
|
||||||
|
end
|
||||||
|
|
||||||
|
test "server uses instructions when configured with protocol version 2025-03-26" do
|
||||||
|
configuration = Configuration.new(protocol_version: "2025-03-26")
|
||||||
|
server = Server.new(name: "test_server", instructions: "The server instructions.", configuration: configuration)
|
||||||
|
assert_equal("The server instructions.", server.instructions)
|
||||||
|
end
|
||||||
|
|
||||||
|
test "raises error if instructions are used with protocol version 2024-11-05" do
|
||||||
|
configuration = Configuration.new(protocol_version: "2024-11-05")
|
||||||
|
|
||||||
|
exception = assert_raises(ArgumentError) do
|
||||||
|
Server.new(name: "test_server", instructions: "The server instructions.", configuration: configuration)
|
||||||
|
end
|
||||||
|
assert_equal("`instructions` supported by protocol version 2025-03-26 or higher", exception.message)
|
||||||
|
end
|
||||||
|
|
||||||
test "#define_tool adds a tool to the server" do
|
test "#define_tool adds a tool to the server" do
|
||||||
@server.define_tool(
|
@server.define_tool(
|
||||||
name: "defined_tool",
|
name: "defined_tool",
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue