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: BasicTypePredicateHandler, FormatPredicateHandler, Generators, NestedRuleHandler, PredicateHandler, RuleTypeDetector, SchemaMetadataExtractor, ServerFiltering, Transports Classes: Logger, Railtie, Resource, SchemaCompiler, Server, Tool

Constant Summary collapse

VERSION =
'1.5.3'

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



73
74
75
76
77
78
79
80
81
82
83
84
85
# File 'lib/fast_mcp.rb', line 73

def self.authenticated_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 FastMcp module
  self.server = server

  server.start_authenticated_rack(app, options)
end

.default_rails_allowed_origins(rail_app) ⇒ Object



170
171
172
173
174
175
176
177
178
179
180
181
182
# File 'lib/fast_mcp.rb', line 170

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



134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
# File 'lib/fast_mcp.rb', line 134

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



186
187
188
# File 'lib/fast_mcp.rb', line 186

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

  • :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



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

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:



106
107
108
109
# File 'lib/fast_mcp.rb', line 106

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:



114
115
116
117
# File 'lib/fast_mcp.rb', line 114

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:



90
91
92
93
# File 'lib/fast_mcp.rb', line 90

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:



98
99
100
101
# File 'lib/fast_mcp.rb', line 98

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