Server class
Annotation to mark a class, library, or method for server generation.
Use this annotation to configure how the server code will be generated for the annotated element. The generator uses these settings to create MCP servers, REST API servers, or both.
The generateMcp parameter controls whether an MCP server implementation is generated. The generateRest parameter controls whether a REST API server and OpenAPI specification are generated.
The transport parameter determines whether the MCP server uses stdio
(for CLI integration) or HTTP (for network access). Only relevant when
generateMcp is true.
The generateJson parameter controls whether the generator should also produce a JSON metadata file alongside the Dart code.
The port parameter specifies the port number for HTTP transport. Only used when transport is McpTransport.http. Defaults to 3000.
The address parameter specifies the bind address for HTTP transport. Only used when transport is McpTransport.http. Defaults to '127.0.0.1'. Use '0.0.0.0' to listen on all interfaces.
The toolPrefix parameter adds a prefix to all tool names in this scope. Useful for organizing tools by domain or avoiding naming collisions when aggregating tools from multiple files.
Example:
@Server(transport: McpTransport.stdio)
@Tool(description: 'Create users')
Future<bool> createUsers(List<User> users) async { ... }
Example with HTTP transport:
@Server(transport: McpTransport.http, port: 8080, address: '0.0.0.0')
@Tool(description: 'Create users')
Future<bool> createUsers(List<User> users) async { ... }
Example with tool prefix:
@Server(transport: McpTransport.stdio, toolPrefix: 'user_service_')
class UserService {
@Tool(description: 'Create user')
Future<User> createUser() async { ... } // Tool name: user_service_createUser
}
Example with auto class prefix:
@Server(transport: McpTransport.stdio, autoClassPrefix: true)
class UserService {
@Tool(description: 'Create user')
Future<User> createUser() async { ... } // Tool name: UserService_createUser
}
Tool-naming resolution order (applied in sequence):
- Base name — either Tool.name if provided, otherwise the Dart method name.
- Auto class prefix — if autoClassPrefix is
trueand the method lives in a class, prependClassName_. - Server tool prefix — if toolPrefix is set, prepend it last.
Combined example showing all three options together:
@Server(
transport: McpTransport.stdio,
autoClassPrefix: true,
toolPrefix: 'api_',
)
class UserService {
// Base: 'createUser' (Dart method name)
// Step 2: 'UserService_createUser' (+ autoClassPrefix)
// Step 3: 'api_UserService_createUser' (+ toolPrefix) ← final tool name
@Tool(description: 'Create user')
Future<User> createUser() async { ... }
// Base: 'user_make' (from @Tool.name, overrides method name)
// Step 2: 'UserService_user_make'
// Step 3: 'api_UserService_user_make' ← final tool name
@Tool(name: 'user_make', description: 'Create user (custom name)')
Future<User> createUserCustom() async { ... }
}
- Annotations
-
- @immutable
Constructors
-
Server({McpTransport transport = McpTransport.stdio, bool generateJson = false, int port = 3000, String address = '127.0.0.1', String? toolPrefix, bool autoClassPrefix = false, bool generateMcp = true, bool generateRest = false, bool codeMode = false, int codeModeTimeout = 30, bool logErrors = false, ToolAnnotations? annotationsDefault, List<
String> corsOrigins = const ['*']}) -
Creates a server configuration annotation.
const
Properties
- address → String
-
The bind address for HTTP transport.
final
- annotationsDefault → ToolAnnotations?
-
Server-wide default values for tool annotation hints.
final
- autoClassPrefix → bool
-
Whether to automatically prefix tool names with their class name.
final
- codeMode → bool
-
Whether to enable code mode for this MCP server.
final
- codeModeTimeout → int
-
Maximum execution time in seconds for code mode scripts.
final
-
corsOrigins
→ List<
String> -
Allowed CORS origins for HTTP transport.
final
- generateJson → bool
-
Whether to generate a JSON metadata file in addition to Dart code.
final
- generateMcp → bool
-
Whether to generate MCP server code (
.mcp.dartand.mcp.json).final - generateRest → bool
-
Whether to generate a REST API server (
.openapi.dartand.openapi.json).final - hashCode → int
-
The hash code for this object.
no setterinherited
- logErrors → bool
-
Whether to log internal errors to stderr for troubleshooting.
final
- port → int
-
The port number for HTTP transport.
final
- runtimeType → Type
-
A representation of the runtime type of the object.
no setterinherited
- toolPrefix → String?
-
Optional prefix for all tool names in this scope.
final
- transport → McpTransport
-
The transport protocol used by the generated MCP server.
final
Methods
-
noSuchMethod(
Invocation invocation) → dynamic -
Invoked when a nonexistent method or property is accessed.
inherited
-
toString(
) → String -
A string representation of this object.
inherited
Operators
-
operator ==(
Object other) → bool -
The equality operator.
inherited