-
Notifications
You must be signed in to change notification settings - Fork 77
Add basic http client support #28
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
koic
merged 1 commit into
modelcontextprotocol:main
from
jcat4:add-basic-http-client-support
Sep 9, 2025
+609
−8
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,88 @@ | ||
| # frozen_string_literal: true | ||
|
|
||
| module MCP | ||
| class Client | ||
| # Initializes a new MCP::Client instance. | ||
| # | ||
| # @param transport [Object] The transport object to use for communication with the server. | ||
| # The transport should be a duck type that responds to `send_request`. See the README for more details. | ||
| # | ||
| # @example | ||
| # transport = MCP::Client::HTTP.new(url: "http://localhost:3000") | ||
| # client = MCP::Client.new(transport: transport) | ||
| def initialize(transport:) | ||
| @transport = transport | ||
| end | ||
|
|
||
| # The user may want to access additional transport-specific methods/attributes | ||
| # So keeping it public | ||
| attr_reader :transport | ||
|
|
||
| # Returns the list of tools available from the server. | ||
| # Each call will make a new request – the result is not cached. | ||
| # | ||
| # @return [Array<MCP::Client::Tool>] An array of available tools. | ||
| # | ||
| # @example | ||
| # tools = client.tools | ||
| # tools.each do |tool| | ||
| # puts tool.name | ||
| # end | ||
| def tools | ||
| response = transport.send_request(request: { | ||
| jsonrpc: JsonRpcHandler::Version::V2_0, | ||
| id: request_id, | ||
| method: "tools/list", | ||
| }) | ||
|
|
||
| response.dig("result", "tools")&.map do |tool| | ||
| Tool.new( | ||
| name: tool["name"], | ||
| description: tool["description"], | ||
| input_schema: tool["inputSchema"], | ||
| ) | ||
| end || [] | ||
| end | ||
|
|
||
| # Calls a tool via the transport layer. | ||
| # | ||
| # @param tool [MCP::Client::Tool] The tool to be called. | ||
| # @param arguments [Object, nil] The arguments to pass to the tool. | ||
| # @return [Object] The result of the tool call, as returned by the transport. | ||
| # | ||
| # @example | ||
| # tool = client.tools.first | ||
| # result = client.call_tool(tool: tool, arguments: { foo: "bar" }) | ||
| # | ||
| # @note | ||
| # The exact requirements for `arguments` are determined by the transport layer in use. | ||
| # Consult the documentation for your transport (e.g., MCP::Client::HTTP) for details. | ||
| def call_tool(tool:, arguments: nil) | ||
| response = transport.send_request(request: { | ||
| jsonrpc: JsonRpcHandler::Version::V2_0, | ||
| id: request_id, | ||
| method: "tools/call", | ||
| params: { name: tool.name, arguments: arguments }, | ||
| }) | ||
|
|
||
| response.dig("result", "content") | ||
| end | ||
|
|
||
| private | ||
|
|
||
| def request_id | ||
| SecureRandom.uuid | ||
| end | ||
|
|
||
| class RequestHandlerError < StandardError | ||
| attr_reader :error_type, :original_error, :request | ||
|
|
||
| def initialize(message, request, error_type: :internal_error, original_error: nil) | ||
| super(message) | ||
| @request = request | ||
| @error_type = error_type | ||
| @original_error = original_error | ||
| end | ||
| end | ||
| end | ||
| end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,88 @@ | ||
| # frozen_string_literal: true | ||
|
|
||
| module MCP | ||
| class Client | ||
| class HTTP | ||
jcat4 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| attr_reader :url | ||
|
|
||
| def initialize(url:, headers: {}) | ||
| @url = url | ||
| @headers = headers | ||
| end | ||
|
|
||
| def send_request(request:) | ||
| method = request[:method] || request["method"] | ||
| params = request[:params] || request["params"] | ||
|
|
||
| client.post("", request).body | ||
| rescue Faraday::BadRequestError => e | ||
| raise RequestHandlerError.new( | ||
| "The #{method} request is invalid", | ||
| { method:, params: }, | ||
| error_type: :bad_request, | ||
| original_error: e, | ||
| ) | ||
| rescue Faraday::UnauthorizedError => e | ||
| raise RequestHandlerError.new( | ||
| "You are unauthorized to make #{method} requests", | ||
| { method:, params: }, | ||
| error_type: :unauthorized, | ||
| original_error: e, | ||
| ) | ||
| rescue Faraday::ForbiddenError => e | ||
| raise RequestHandlerError.new( | ||
| "You are forbidden to make #{method} requests", | ||
| { method:, params: }, | ||
| error_type: :forbidden, | ||
| original_error: e, | ||
| ) | ||
| rescue Faraday::ResourceNotFound => e | ||
| raise RequestHandlerError.new( | ||
| "The #{method} request is not found", | ||
| { method:, params: }, | ||
| error_type: :not_found, | ||
| original_error: e, | ||
| ) | ||
| rescue Faraday::UnprocessableEntityError => e | ||
| raise RequestHandlerError.new( | ||
| "The #{method} request is unprocessable", | ||
| { method:, params: }, | ||
| error_type: :unprocessable_entity, | ||
| original_error: e, | ||
| ) | ||
| rescue Faraday::Error => e # Catch-all | ||
| raise RequestHandlerError.new( | ||
| "Internal error handling #{method} request", | ||
| { method:, params: }, | ||
| error_type: :internal_error, | ||
| original_error: e, | ||
| ) | ||
| end | ||
|
|
||
| private | ||
|
|
||
| attr_reader :headers | ||
|
|
||
| def client | ||
| require_faraday! | ||
| @client ||= Faraday.new(url) do |faraday| | ||
| faraday.request(:json) | ||
| faraday.response(:json) | ||
| faraday.response(:raise_error) | ||
|
|
||
| headers.each do |key, value| | ||
| faraday.headers[key] = value | ||
| end | ||
| end | ||
| end | ||
|
|
||
| def require_faraday! | ||
| require "faraday" | ||
| rescue LoadError | ||
| raise LoadError, "The 'faraday' gem is required to use the MCP client HTTP transport. " \ | ||
| "Add it to your Gemfile: gem 'faraday', '>= 2.0'" \ | ||
| "See https://rubygems.org/gems/faraday for more details." | ||
| end | ||
| end | ||
| end | ||
| end | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,15 @@ | ||
| # frozen_string_literal: true | ||
|
|
||
| module MCP | ||
| class Client | ||
| class Tool | ||
| attr_reader :name, :description, :input_schema | ||
|
|
||
| def initialize(name:, description:, input_schema:) | ||
| @name = name | ||
| @description = description | ||
| @input_schema = input_schema | ||
| end | ||
| end | ||
| end | ||
| end |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
At the moment, this is server-specific. If we have this patch a client config too (or stop having the config scoped to just the server), we can move this somewhere else in the README