Module: FastMcp

Defined in:
lib/fast_mcp.rb,
lib/fast_mcp.rb,
lib/mcp/tool.rb,
lib/mcp/logger.rb,
lib/mcp/server.rb,
lib/mcp/railtie.rb,
lib/mcp/version.rb,
lib/mcp/resource.rb,
lib/mcp/server_filtering.rb,
lib/mcp/transports/base_transport.rb,
lib/mcp/transports/rack_transport.rb,
lib/mcp/transports/stdio_transport.rb,
lib/mcp/transports/authenticated_rack_transport.rb,
lib/generators/fast_mcp/install/install_generator.rb

Overview

This class is not used yet.

Defined Under Namespace

Modules: Generators, ServerFiltering, Transports Classes: Logger, Railtie, Resource, Server, Tool

Constant Summary collapse

VERSION =
'1.6.0'

Class Attribute Summary collapse

Class Method Summary collapse

Class Attribute Details

.serverObject

Returns the value of attribute server.



9
10
11
# File 'lib/fast_mcp.rb', line 9

def server
  @server
end

Class Method Details

.authenticated_rack_middleware(app, options = {}) {|server| ... } ⇒ #call

Create a Rack middleware for the MCP server with authentication

Parameters:

  • app (#call)

    The Rack application

  • options (Hash) (defaults to: {})

    Options for the middleware

Options Hash (options):

  • :name (String)

    The name of the server

  • :version (String)

    The version of the server

  • :auth_token (String)

    The authentication token

  • :allowed_origins (Array<String,Regexp>)

    List of allowed origins for DNS rebinding protection

Yields:

  • (server)

    A block to configure the server

Yield Parameters:

Returns:

  • (#call)

    The Rack middleware



71
72
73
74
# File 'lib/fast_mcp.rb', line 71

def self.authenticated_rack_middleware(app, options = {})
  options[:transport] ||= FastMcp::Transports::AuthenticatedRackTransport
  rack_middleware(app, options)
end

.default_rails_allowed_origins(rail_app) ⇒ Object



159
160
161
162
163
164
165
166
167
168
169
170
171
# File 'lib/fast_mcp.rb', line 159

def self.default_rails_allowed_origins(rail_app)
  hosts = rail_app.config.hosts

  hosts.map do |host|
    if host.is_a?(String) && host.start_with?('.')
      # Convert .domain to domain and *.domain
      host_without_dot = host[1..]
      [host_without_dot, Regexp.new(".*\.#{host_without_dot}")] # rubocop:disable Style/RedundantStringEscape
    else
      host
    end
  end.flatten.compact
end

.mount_in_rails(app, options = {}) {|server| ... } ⇒ #call

Mount the MCP middleware in a Rails application

Parameters:

  • app (Rails::Application)

    The Rails application

  • options (Hash) (defaults to: {})

    Options for the middleware

Options Hash (options):

  • :name (String)

    The name of the server

  • :version (String)

    The version of the server

  • :path_prefix (String)

    The path prefix for the MCP endpoints

  • :messages_route (String)

    The route for the messages endpoint

  • :sse_route (String)

    The route for the SSE endpoint

  • :logger (Logger)

    The logger to use

  • :authenticate (Boolean)

    Whether to use authentication

  • :auth_token (String)

    The authentication token

  • :allowed_origins (Array<String,Regexp>)

    List of allowed origins for DNS rebinding protection

Yields:

  • (server)

    A block to configure the server

Yield Parameters:

Returns:

  • (#call)

    The Rack middleware



123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
# File 'lib/fast_mcp.rb', line 123

def self.mount_in_rails(app, options = {})
  # Default options
  name = options.delete(:name) || app.class.module_parent_name.underscore.dasherize
  version = options.delete(:version) || '1.0.0'
  logger = options[:logger] || Rails.logger
  path_prefix = options.delete(:path_prefix) || '/mcp'
  messages_route = options.delete(:messages_route) || 'messages'
  sse_route = options.delete(:sse_route) || 'sse'
  authenticate = options.delete(:authenticate) || false
  allowed_origins = options[:allowed_origins] || default_rails_allowed_origins(app)
  allowed_ips = options[:allowed_ips] || FastMcp::Transports::RackTransport::DEFAULT_ALLOWED_IPS

  options[:localhost_only] = Rails.env.local? if options[:localhost_only].nil?
  options[:allowed_ips] = allowed_ips
  options[:logger] = logger
  options[:allowed_origins] = allowed_origins

  # Create or get the server
  self.server = FastMcp::Server.new(name: name, version: version, logger: logger)
  yield self.server if block_given?

  # Choose the right middleware based on authentication
  self.server.transport_klass = if authenticate
                                  FastMcp::Transports::AuthenticatedRackTransport
                                else
                                  FastMcp::Transports::RackTransport
                                end

  # Insert the middleware in the Rails middleware stack
  app.middleware.use(
    self.server.transport_klass,
    self.server,
    options.merge(path_prefix: path_prefix, messages_route: messages_route, sse_route: sse_route)
  )
end

.notify_resource_updated(uri) ⇒ Object

Notify the server that a resource has been updated

Parameters:

  • uri (String)

    The URI of the resource



175
176
177
# File 'lib/fast_mcp.rb', line 175

def self.notify_resource_updated(uri)
  self.server.notify_resource_updated(uri)
end

.rack_middleware(app, options = {}) {|server| ... } ⇒ #call

Create a Rack middleware for the MCP server

Parameters:

  • app (#call)

    The Rack application

  • options (Hash) (defaults to: {})

    Options for the middleware

Options Hash (options):

  • :name (String)

    The name of the server

  • :version (String)

    The version of the server

  • :path_prefix (String)

    The path prefix for the MCP endpoints

  • :messages_route (String)

    The route for the messages endpoint

  • :sse_route (String)

    The route for the SSE endpoint

  • :logger (Logger)

    The logger to use

  • :transport (Class)

    The transport class to use

  • :allowed_origins (Array<String,Regexp>)

    List of allowed origins for DNS rebinding protection

Yields:

  • (server)

    A block to configure the server

Yield Parameters:

Returns:

  • (#call)

    The Rack middleware



44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/fast_mcp.rb', line 44

def self.rack_middleware(app, options = {})
  name = options.delete(:name) || 'mcp-server'
  version = options.delete(:version) || '1.0.0'
  logger = options.delete(:logger) || Logger.new

  server = FastMcp::Server.new(name: name, version: version, logger: logger)
  yield server if block_given?

  # Store the server in the Sinatra settings if available
  app.settings.set(:mcp_server, server) if app.respond_to?(:settings) && app.settings.respond_to?(:mcp_server=)

  # Store the server in the FastMcp module
  self.server = server

  server.start_rack(app, options)
end

.register_resource(resource) ⇒ FastMcp::Resource

Register a resource with the MCP server

Parameters:

Returns:



95
96
97
98
# File 'lib/fast_mcp.rb', line 95

def self.register_resource(resource)
  self.server ||= FastMcp::Server.new(name: 'mcp-server', version: '1.0.0')
  self.server.register_resource(resource)
end

.register_resources(*resources) ⇒ Array<FastMcp::Resource>

Register multiple resources at once

Parameters:

Returns:



103
104
105
106
# File 'lib/fast_mcp.rb', line 103

def self.register_resources(*resources)
  self.server ||= FastMcp::Server.new(name: 'mcp-server', version: '1.0.0')
  self.server.register_resources(*resources)
end

.register_tool(tool) ⇒ FastMcp::Tool

Register a tool with the MCP server

Parameters:

Returns:



79
80
81
82
# File 'lib/fast_mcp.rb', line 79

def self.register_tool(tool)
  self.server ||= FastMcp::Server.new(name: 'mcp-server', version: '1.0.0')
  self.server.register_tool(tool)
end

.register_tools(*tools) ⇒ Array<FastMcp::Tool>

Register multiple tools at once

Parameters:

Returns:



87
88
89
90
# File 'lib/fast_mcp.rb', line 87

def self.register_tools(*tools)
  self.server ||= FastMcp::Server.new(name: 'mcp-server', version: '1.0.0')
  self.server.register_tools(*tools)
end