active_mcp 0.1.1 → 0.2.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: f768454eb918802f12f22205183172abcddeee402f1747c0223810e0622945f5
4
- data.tar.gz: 336a6e84d15e9899240ef15de5912063cfe7729848a3617cf584cf987603d4d4
3
+ metadata.gz: f94cddd4a196e9f5d1b0501457cb9d318fe1a32cfb997391f0b428f42b850813
4
+ data.tar.gz: de38f78d65269af6441c9f375ebca7ae2372acbee26c87ac04d68815a844da07
5
5
  SHA512:
6
- metadata.gz: 5a9db4bc9729b90fdc34caf1aae7715c73bb9596162641d34e5ca0ca12f03cac1b1b8576def4198bdc59d1967a4184377a1ee9dbf050d73d82c69385b8a2df40
7
- data.tar.gz: 7027f29f2402272a75cce1e96791e4218b006558787efdbe6aa0ddadb93bc0ff84aa82afc36523eda1b16ba5d264f6e5be8df47316adb9f1b69d3b60ab691ddf
6
+ metadata.gz: 17f8847a39433c08b61838f416dfa6f3e0aecce6daf6ef8051dffdb5e71ac58f191c7654080570f18aae88b002219efd06a260d3afd26d7d1724a5758abf8746
7
+ data.tar.gz: afc770724f80df1f92929ab69c88577a6a0e974ad0647acf2dbdf677c7d6dc5b0a01f8792d53ef7eac818a72e11346769f13cd64f6651cc6375ba1bb544c8099
data/README.md CHANGED
@@ -136,7 +136,43 @@ http://your-app.example.com/mcp
136
136
 
137
137
  Clients will discover the available tools and their input schemas automatically through the MCP protocol.
138
138
 
139
- ## Authentication Flow
139
+ ## Authorization & Authentication
140
+
141
+ ActiveMcp supports both authentication (verifying who a user is) and authorization (controlling what resources they can access).
142
+
143
+ ### Authorization for Tools
144
+
145
+ You can control which tools are visible and accessible to different users by overriding the `authorized?` class method:
146
+
147
+ ```ruby
148
+ class AdminOnlyTool < ActiveMcp::Tool
149
+ description "This tool is only accessible by admins"
150
+
151
+ property :command, :string, required: true, description: "Admin command to execute"
152
+
153
+ # Define authorization logic - only admin tokens can access this tool
154
+ def self.authorized?(auth_info)
155
+ return false unless auth_info
156
+ return false unless auth_info[:type] == :bearer
157
+
158
+ # Check if the token belongs to an admin
159
+ auth_info[:token] == "admin-token" || User.find_by_token(auth_info[:token])&.admin?
160
+ end
161
+
162
+ def call(command:, auth_info: nil)
163
+ # Tool implementation
164
+ end
165
+ end
166
+ ```
167
+
168
+ When a user makes a request to the MCP server:
169
+ 1. Only tools that return `true` from their `authorized?` method will be included in the tools list
170
+ 2. Users can only call tools that they're authorized to use
171
+ 3. Unauthorized access attempts will return a 403 Forbidden response
172
+
173
+ This makes it easy to create role-based access control for your MCP tools.
174
+
175
+ ### Authentication Flow
140
176
 
141
177
  ActiveMcp supports receiving authentication credentials from MCP clients and forwarding them to your Rails application. There are two ways to handle authentication:
142
178
 
@@ -37,7 +37,11 @@ module ActiveMcp
37
37
  end
38
38
 
39
39
  def render_tools_list
40
- tools = Tool.registered_tools.map do |tool_class|
40
+ # 認可チェックを含めてツールリストをフィルタリング
41
+ tools = Tool.registered_tools.select do |tool_class|
42
+ # 認可チェック - ツールがauthorized?メソッドでtrueを返すもののみを選択
43
+ tool_class.authorized?(@auth_info)
44
+ end.map do |tool_class|
41
45
  {
42
46
  name: tool_class.tool_name,
43
47
  description: tool_class.desc,
@@ -65,6 +69,12 @@ module ActiveMcp
65
69
  render json: {error: "Tool not found: #{tool_name}"}, status: 404
66
70
  return
67
71
  end
72
+
73
+ # 認可チェック
74
+ unless tool_class.authorized?(@auth_info)
75
+ render json: {error: "Unauthorized: Access to tool '#{tool_name}' denied"}, status: 403
76
+ return
77
+ end
68
78
 
69
79
  tool = tool_class.new
70
80
  validation_result = tool.validate_arguments(arguments)
@@ -34,6 +34,10 @@ module ActiveMcp
34
34
  def inherited(subclass)
35
35
  registered_tools << subclass
36
36
  end
37
+
38
+ def authorized?(auth_info)
39
+ true
40
+ end
37
41
  end
38
42
 
39
43
  def initialize
@@ -1,3 +1,3 @@
1
1
  module ActiveMcp
2
- VERSION = "0.1.1"
2
+ VERSION = "0.2.1"
3
3
  end
@@ -5,6 +5,23 @@ class <%= class_name %> < ActiveMcp::Tool
5
5
  property :param2, :string, required: false, description: "Second parameter description"
6
6
  # Add more parameters as needed
7
7
 
8
+ # Uncomment and modify this method to implement authorization control
9
+ # This controls who can see and use this tool
10
+ # def self.authorized?(auth_info)
11
+ # # Example: require authentication
12
+ # # return false unless auth_info
13
+ #
14
+ # # Example: require a specific authentication type
15
+ # # return false unless auth_info[:type] == :bearer
16
+ #
17
+ # # Example: check for admin permissions
18
+ # # admin_tokens = ["admin-token"]
19
+ # # return admin_tokens.include?(auth_info[:token])
20
+ #
21
+ # # Default: allow all access
22
+ # true
23
+ # end
24
+
8
25
  def call(param1:, param2: nil, auth_info: nil, **args)
9
26
  # Authentication information can be accessed via _auth_info parameter
10
27
  # auth_info = { type: :bearer, token: "xxx", header: "Bearer xxx" }
metadata CHANGED
@@ -1,13 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: active_mcp
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.1
4
+ version: 0.2.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Your Name
8
8
  bindir: bin
9
9
  cert_chain: []
10
- date: 2025-04-03 00:00:00.000000000 Z
10
+ date: 2025-04-04 00:00:00.000000000 Z
11
11
  dependencies:
12
12
  - !ruby/object:Gem::Dependency
13
13
  name: rails