executeImpl method

  1. @override
Future executeImpl(
  1. Map<String, dynamic> params
)
override

Implement the actual tool logic

params are the validated parameters (never null here) Returns the result data to send in the response

Implementation

@override
Future<dynamic> executeImpl(Map<String, dynamic> params) async {
  final endpoint = params['endpoint'] as String;
  final method = params['method'] as String;
  final parameters = params['parameters'] as Map<String, dynamic>?;

  final project = ServerPodLocator.getProject();
  if (project == null || !project.isValid) {
    return {'error': 'Not a valid ServerPod project'};
  }

  // Find the endpoint
  String? endpointFile;
  for (final file in project.endpointFiles) {
    final name = _extractEndpointName(file);
    if (name == endpoint) {
      endpointFile = file;
      break;
    }
  }

  if (endpointFile == null) {
    return {
      'error': 'Endpoint not found',
      'endpoint': endpoint,
      'available': project.endpointFiles.map(_extractEndpointName).toList(),
    };
  }

  // Parse methods
  final methods = MethodParser.parseFile(endpointFile);
  final targetMethod = methods.firstWhere(
    (m) => m.name == method,
    orElse: () => throw Exception('Method not found'),
  );

  // Validate parameters
  final userParams = targetMethod.userParameters;
  final paramErrors = <String>[];

  for (final param in userParams) {
    if (parameters != null && !parameters.containsKey(param.name)) {
      paramErrors.add('Missing required parameter: ${param.name}');
    }
  }

  if (paramErrors.isNotEmpty) {
    return {
      'error': 'Parameter validation failed',
      'errors': paramErrors,
      'expectedParameters': userParams.map((p) => {
        'name': p.name,
        'type': p.type,
      }).toList(),
    };
  }

  // Return method info (placeholder for actual execution)
  return {
    'status': 'placeholder',
    'message': 'Endpoint calling not yet implemented',
    'endpoint': endpoint,
    'method': method,
    'signature': targetMethod.signature,
    'returnType': targetMethod.returnType,
    'parameters': parameters,
    'note': 'This would call the endpoint method in a future implementation',
  };
}