Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 10 additions & 7 deletions example/iostream-client-server/server_iostream.dart
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,17 @@ Future<McpServer> getServer() async {
mcpServer.tool(
"calculate",
description: 'Perform basic arithmetic operations',
inputSchemaProperties: {
'operation': {
'type': 'string',
'enum': ['add', 'subtract', 'multiply', 'divide'],
toolInputSchema: ToolInputSchema(
properties: {
'operation': {
'type': 'string',
'enum': ['add', 'subtract', 'multiply', 'divide'],
},
'a': {'type': 'number'},
'b': {'type': 'number'},
},
'a': {'type': 'number'},
'b': {'type': 'number'},
},
required: ['operation', 'a', 'b'],
),
callback: ({args, extra}) async {
final operation = args!['operation'];
final a = args['a'];
Expand Down
17 changes: 10 additions & 7 deletions example/server_sse.dart
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,17 @@ Future<void> main() async {
mcpServer.tool(
"calculate",
description: 'Perform basic arithmetic operations',
inputSchemaProperties: {
'operation': {
'type': 'string',
'enum': ['add', 'subtract', 'multiply', 'divide'],
toolInputSchema: ToolInputSchema(
properties: {
'operation': {
'type': 'string',
'enum': ['add', 'subtract', 'multiply', 'divide'],
},
'a': {'type': 'number'},
'b': {'type': 'number'},
},
'a': {'type': 'number'},
'b': {'type': 'number'},
},
required: ['operation', 'a', 'b'],
),
callback: ({args, extra}) async {
final operation = args!['operation'];
final a = args['a'];
Expand Down
17 changes: 10 additions & 7 deletions example/server_stdio.dart
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,17 @@ void main() async {
server.tool(
'calculate',
description: 'Perform basic arithmetic operations',
inputSchemaProperties: {
'operation': {
'type': 'string',
'enum': ['add', 'subtract', 'multiply', 'divide'],
toolInputSchema: ToolInputSchema(
properties: {
'operation': {
'type': 'string',
'enum': ['add', 'subtract', 'multiply', 'divide'],
},
'a': {'type': 'number'},
'b': {'type': 'number'},
},
'a': {'type': 'number'},
'b': {'type': 'number'},
},
required: ['operation', 'a', 'b'],
),
callback: ({args, extra}) async {
final operation = args!['operation'];
final a = args['a'];
Expand Down
48 changes: 31 additions & 17 deletions example/streamable_https/server_streamable_https.dart
Original file line number Diff line number Diff line change
Expand Up @@ -67,9 +67,15 @@ McpServer getServer() {
server.tool(
'greet',
description: 'A simple greeting tool',
inputSchemaProperties: {
'name': {'type': 'string', 'description': 'Name to greet'},
},
toolInputSchema: ToolInputSchema(
properties: {
'name': {
'type': 'string',
'description': 'Name to greet',
},
},
required: ['name'],
),
callback: ({args, extra}) async {
final name = args?['name'] as String? ?? 'world';
return CallToolResult.fromContent(
Expand All @@ -85,9 +91,15 @@ McpServer getServer() {
'multi-greet',
description:
'A tool that sends different greetings with delays between them',
inputSchemaProperties: {
'name': {'type': 'string', 'description': 'Name to greet'},
},
toolInputSchema: ToolInputSchema(
properties: {
'name': {
'type': 'string',
'description': 'Name to greet',
},
},
required: [],
),
annotations: ToolAnnotations(
title: 'Multiple Greeting Tool',
readOnlyHint: true,
Expand Down Expand Up @@ -162,18 +174,20 @@ McpServer getServer() {
'start-notification-stream',
description:
'Starts sending periodic notifications for testing resumability',
inputSchemaProperties: {
'interval': {
'type': 'number',
'description': 'Interval in milliseconds between notifications',
'default': 100,
},
'count': {
'type': 'number',
'description': 'Number of notifications to send (0 for 100)',
'default': 50,
toolInputSchema: ToolInputSchema(
properties: {
'interval': {
'type': 'number',
'description': 'Interval in milliseconds between notifications',
'default': 100,
},
'count': {
'type': 'number',
'description': 'Number of notifications to send (0 for 100)',
'default': 50,
},
},
},
),
callback: ({args, extra}) async {
final interval = args?['interval'] as num? ?? 100;
final count = args?['count'] as num? ?? 50;
Expand Down
31 changes: 20 additions & 11 deletions example/weather.dart
Original file line number Diff line number Diff line change
Expand Up @@ -53,12 +53,15 @@ void main() async {
server.tool(
"get-alerts",
description: "Get weather alerts for a state",
inputSchemaProperties: {
"state": {
"type": "string",
"description": "Two-letter state code (e.g. CA, NY)",
toolInputSchema: ToolInputSchema(
properties: {
"state": {
"type": "string",
"description": "Two-letter state code (e.g. CA, NY)",
},
},
},
required: ["state"],
),
callback: ({args, extra}) async {
final state = (args?['state'] as String?)?.toUpperCase();
if (state == null || state.length != 2) {
Expand Down Expand Up @@ -98,13 +101,19 @@ void main() async {
server.tool(
"get-forecast",
description: "Get weather forecast for a location",
inputSchemaProperties: {
"latitude": {"type": "number", "description": "Latitude of the location"},
"longitude": {
"type": "number",
"description": "Longitude of the location",
toolInputSchema: ToolInputSchema(
properties: {
"latitude": {
"type": "number",
"description": "Latitude of the location",
},
"longitude": {
"type": "number",
"description": "Longitude of the location",
},
},
},
required: ["latitude", "longitude"],
),
callback: ({args, extra}) async {
final latitude = args?['latitude'] as num?;
final longitude = args?['longitude'] as num?;
Expand Down
28 changes: 18 additions & 10 deletions lib/src/server/mcp.dart
Original file line number Diff line number Diff line change
Expand Up @@ -82,15 +82,15 @@ class ResourceTemplateRegistration {

class _RegisteredTool {
final String? description;
final Map<String, dynamic>? inputSchemaProperties;
final Map<String, dynamic>? outputSchemaProperties;
final ToolInputSchema? toolInputSchema;
final ToolOutputSchema? toolOutputSchema;
final ToolAnnotations? annotations;
final ToolCallback callback;

const _RegisteredTool({
this.description,
this.inputSchemaProperties,
this.outputSchemaProperties,
this.toolInputSchema,
this.toolOutputSchema,
this.annotations,
required this.callback,
});
Expand All @@ -99,11 +99,9 @@ class _RegisteredTool {
return Tool(
name: name,
description: description,
inputSchema: ToolInputSchema(properties: inputSchemaProperties),
inputSchema: toolInputSchema ?? ToolInputSchema(),
// Do not include output schema in the payload if it isn't defined
outputSchema: outputSchemaProperties != null
? ToolOutputSchema(properties: outputSchemaProperties)
: null,
outputSchema: toolOutputSchema,
annotations: annotations,
);
}
Expand Down Expand Up @@ -567,7 +565,11 @@ class McpServer {
void tool(
String name, {
String? description,
ToolInputSchema? toolInputSchema,
ToolOutputSchema? toolOutputSchema,
@Deprecated('Use toolInputSchema instead')
Map<String, dynamic>? inputSchemaProperties,
@Deprecated('Use toolOutputSchema instead')
Map<String, dynamic>? outputSchemaProperties,
ToolAnnotations? annotations,
required ToolCallback callback,
Expand All @@ -577,8 +579,14 @@ class McpServer {
}
_registeredTools[name] = _RegisteredTool(
description: description,
inputSchemaProperties: inputSchemaProperties,
outputSchemaProperties: outputSchemaProperties,
toolInputSchema: toolInputSchema ??
(inputSchemaProperties != null
? ToolInputSchema(properties: inputSchemaProperties)
: null),
toolOutputSchema: toolOutputSchema ??
(outputSchemaProperties != null
? ToolOutputSchema(properties: outputSchemaProperties)
: null),
annotations: annotations,
callback: callback,
);
Expand Down