@@ -54,57 +54,6 @@ It implements the Model Context Protocol specification, handling model context r
5454- ` completion/complete ` - Returns autocompletion suggestions for prompt arguments and resource URIs
5555- ` sampling/createMessage ` - Requests LLM completion from the client (server-to-client)
5656
57- ### Custom Methods
58-
59- The server allows you to define custom JSON-RPC methods beyond the standard MCP protocol methods using the ` define_custom_method ` method:
60-
61- ``` ruby
62- server = MCP ::Server .new (name: " my_server" )
63-
64- # Define a custom method that returns a result
65- server.define_custom_method(method_name: " add" ) do |params |
66- params[:a ] + params[:b ]
67- end
68-
69- # Define a custom notification method (returns nil)
70- server.define_custom_method(method_name: " notify" ) do |params |
71- # Process notification
72- nil
73- end
74- ```
75-
76- ** Key Features:**
77-
78- - Accepts any method name as a string
79- - Block receives the request parameters as a hash
80- - Can handle both regular methods (with responses) and notifications
81- - Prevents overriding existing MCP protocol methods
82- - Supports instrumentation callbacks for monitoring
83-
84- ** Usage Example:**
85-
86- ``` ruby
87- # Client request
88- {
89- " jsonrpc" : " 2.0" ,
90- " id" : 1 ,
91- " method" : " add" ,
92- " params" : { " a" : 5 , " b" : 3 }
93- }
94-
95- # Server response
96- {
97- " jsonrpc" : " 2.0" ,
98- " id" : 1 ,
99- " result" : 8
100- }
101- ```
102-
103- ** Error Handling:**
104-
105- - Raises ` MCP::Server::MethodAlreadyDefinedError ` if trying to override an existing method
106- - Supports the same exception reporting and instrumentation as standard methods
107-
10857### Sampling
10958
11059The Model Context Protocol allows servers to request LLM completions from clients through the ` sampling/createMessage ` method.
@@ -502,6 +451,59 @@ When configured, sessions that receive no HTTP requests for this duration are au
502451transport = MCP ::Server ::Transports ::StreamableHTTPTransport .new (server, session_idle_timeout: 1800 )
503452```
504453
454+ ### Advanced
455+
456+ #### Custom Methods
457+
458+ The server allows you to define custom JSON-RPC methods beyond the standard MCP protocol methods using the ` define_custom_method ` method:
459+
460+ ``` ruby
461+ server = MCP ::Server .new (name: " my_server" )
462+
463+ # Define a custom method that returns a result
464+ server.define_custom_method(method_name: " add" ) do |params |
465+ params[:a ] + params[:b ]
466+ end
467+
468+ # Define a custom notification method (returns nil)
469+ server.define_custom_method(method_name: " notify" ) do |params |
470+ # Process notification
471+ nil
472+ end
473+ ```
474+
475+ ** Key Features:**
476+
477+ - Accepts any method name as a string
478+ - Block receives the request parameters as a hash
479+ - Can handle both regular methods (with responses) and notifications
480+ - Prevents overriding existing MCP protocol methods
481+ - Supports instrumentation callbacks for monitoring
482+
483+ ** Usage Example:**
484+
485+ ``` ruby
486+ # Client request
487+ {
488+ " jsonrpc" : " 2.0" ,
489+ " id" : 1 ,
490+ " method" : " add" ,
491+ " params" : { " a" : 5 , " b" : 3 }
492+ }
493+
494+ # Server response
495+ {
496+ " jsonrpc" : " 2.0" ,
497+ " id" : 1 ,
498+ " result" : 8
499+ }
500+ ```
501+
502+ ** Error Handling:**
503+
504+ - Raises ` MCP::Server::MethodAlreadyDefinedError ` if trying to override an existing method
505+ - Supports the same exception reporting and instrumentation as standard methods
506+
505507### Unsupported Features (to be implemented in future versions)
506508
507509- Resource subscriptions
0 commit comments