Skip to content

A 'pub-dev-search' mcp tool #103

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 59 commits into from
May 19, 2025
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
59 commits
Select commit Hold shift + click to select a range
cf1b4b4
A pub search functionality
sigurdm May 2, 2025
65de883
Update pkgs/dart_tooling_mcp_server/lib/src/mixins/pub_dev_search.dart
sigurdm May 5, 2025
dbcabff
Update pkgs/dart_tooling_mcp_server/lib/src/mixins/pub_dev_search.dart
sigurdm May 5, 2025
ff7cc43
Detailed search term description
sigurdm May 5, 2025
a383fb5
Rename argument `search-query` -> `query`
sigurdm May 5, 2025
0fe6f8c
Remove unused import
sigurdm May 5, 2025
5426861
Underscores in tool name
sigurdm May 5, 2025
245d790
Readonlyhint: true
sigurdm May 5, 2025
8f5e9cb
Remove copy-paste waste
sigurdm May 5, 2025
ce713a1
Extract `dig` json utility
sigurdm May 5, 2025
cd3ba90
Remove unused query parameters
sigurdm May 5, 2025
f9219b4
Make client mockable
sigurdm May 5, 2025
f1f9f2c
Add test
sigurdm May 5, 2025
2b28b8d
Better golden file error message
sigurdm May 5, 2025
084cfe7
Privatify name
sigurdm May 5, 2025
c803a7b
Test failure
sigurdm May 5, 2025
e0afe2d
Run subqueries in parallel
sigurdm May 5, 2025
29888c8
Test and fix json dig
sigurdm May 5, 2025
7442c9c
Make pool top-level static
sigurdm May 5, 2025
9070d8a
Update pkgs/dart_tooling_mcp_server/test_fixtures/pub_dev_responses/R…
sigurdm May 5, 2025
b46121c
Update pkgs/dart_tooling_mcp_server/test_fixtures/pub_dev_responses/R…
sigurdm May 5, 2025
9e1283a
Update pkgs/dart_tooling_mcp_server/lib/src/utils/json.dart
sigurdm May 6, 2025
ab08279
json util, handle specified types
sigurdm May 6, 2025
77ccf09
Update pkgs/dart_tooling_mcp_server/lib/src/mixins/pub_dev_search.dart
sigurdm May 6, 2025
95da176
Handle generic types
sigurdm May 6, 2025
7a4c89c
Remove nonsense
sigurdm May 6, 2025
525ea44
Bump subosito/flutter-action from 1 to 2 in the github-actions group …
dependabot[bot] May 1, 2025
2093225
Making `instructions` optional. (#98)
domesticmouse May 1, 2025
b10e03c
Require roots for all CLI tools (#101)
jakemac53 May 2, 2025
29cd5b2
Add runtime errors resource and tool to clear errors. (#94)
jakemac53 May 2, 2025
adf7ee9
add option to log protocol messages to a Sink<String> (#102)
jakemac53 May 2, 2025
fed9134
Modify DTD connection verbiage in example client (#104)
kenzieschmoll May 2, 2025
4871a32
add test for server closing early to validate behavior (#105)
jakemac53 May 2, 2025
06c71c7
Continue making `instructions` optional. (#107)
domesticmouse May 4, 2025
23aa993
Release dart_mcp version 0.2.0 (#106)
jakemac53 May 5, 2025
009219c
Add supported tools to the MCP server README (#109)
kenzieschmoll May 5, 2025
346d7c4
add signature_help tool (#110)
jakemac53 May 5, 2025
f5c83bd
show thinking text and input/output token usage (#108)
jakemac53 May 5, 2025
cc41bfb
Tolerate missing sub-responses
sigurdm May 6, 2025
e483a8c
Report error when no packages found
sigurdm May 6, 2025
472d00b
merge
sigurdm May 6, 2025
b30e7f9
dartfmt
sigurdm May 6, 2025
10c723d
Extract result count constant
sigurdm May 9, 2025
1f06c85
End with period
sigurdm May 9, 2025
0745135
Use named records
sigurdm May 9, 2025
152457d
End with period
sigurdm May 9, 2025
524fac2
Break up lines
sigurdm May 9, 2025
b9d78aa
Extract test helper
sigurdm May 9, 2025
c3b41d8
Explain no 'or' operator in tool description
sigurdm May 12, 2025
d36f0f5
Merge
sigurdm May 12, 2025
5636292
Fix merge
sigurdm May 12, 2025
d0cdb29
Sort dependencies
sigurdm May 12, 2025
1e4524e
Address some of review
sigurdm May 13, 2025
e7f3935
Fix rename
sigurdm May 13, 2025
e3e187e
Refactor to use `runWithClient`
sigurdm May 13, 2025
9e6e34b
dynamic -> Object
sigurdm May 13, 2025
b808801
Use list of strings instead of list of objects
sigurdm May 16, 2025
6c98900
Restrict max number of listed identifiers
sigurdm May 16, 2025
a9c13b9
Report the publisher of the package
sigurdm May 16, 2025
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
265 changes: 265 additions & 0 deletions pkgs/dart_tooling_mcp_server/lib/src/mixins/pub_dev_search.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,265 @@
// Copyright (c) 2025, the Dart project authors. Please see the AUTHORS file
// for details. All rights reserved. Use of this source code is governed by a
// BSD-style license that can be found in the LICENSE file.

import 'dart:async';
import 'dart:convert';

import 'package:dart_mcp/server.dart';
import 'package:http/http.dart';

import '../utils/process_manager.dart';

/// Mix this in to any MCPServer to add support for doing searched on pub.dev.
base mixin PubDevSupport on ToolsSupport, LoggingSupport
implements ProcessManagerSupport {
@override
FutureOr<InitializeResult> initialize(InitializeRequest request) {
registerTool(pubDevTool, _runPubDevSearch);
return super.initialize(request);
}

/// Implementation of the [pubDevTool].
Future<CallToolResult> _runPubDevSearch(CallToolRequest request) async {
final query = request.arguments?['search-query'] as String?;
if (query == null) {
return CallToolResult(
content: [
TextContent(text: 'Missing required argument `search-query`.'),
],
isError: true,
);
}
final client = Client();
final searchUrl = Uri(
scheme: 'https',
host: 'pub.dev',
path: 'api/search',
queryParameters: {'q': query},
);
final Object? result;
try {
result = jsonDecode(await client.read(searchUrl));

final packages = dig<List>(result, ['packages']);

final results = <TextContent>[];
for (final i in (packages as Iterable).take(10)) {
final packageName = dig<String>(i, ['package']);
final packageListing = jsonDecode(
await client.read(
Uri(
scheme: 'https',
host: 'pub.dev',
path: 'api/packages/$packageName',
queryParameters: {'q': query},
),
),
);

final latestVersion = dig<String>(packageListing, [
'latest',
'version',
]);
final description = dig<String>(packageListing, [
'latest',
'pubspec',
'description',
]);
final scoreResult = jsonDecode(
await client.read(
Uri(
scheme: 'https',
host: 'pub.dev',
path: 'api/packages/$packageName/score',
queryParameters: {'q': query},
),
),
);
final scores = {
'pubPoints': dig<int>(scoreResult, ['grantedPoints']),
'maxPubPoints': dig<int>(scoreResult, ['maxPoints']),
'likes': dig<int>(scoreResult, ['likeCount']),
'downloadCount': dig<int>(scoreResult, ['downloadCount30Days']),
};
final topics =
dig<List>(scoreResult, [
'tags',
]).where((t) => (t as String).startsWith('topic:')).toList();
final licenses =
dig<List>(scoreResult, [
'tags',
]).where((t) => (t as String).startsWith('license')).toList();
final index = jsonDecode(
await client.read(
Uri(
scheme: 'https',
host: 'pub.dev',
path: 'documentation/$packageName/latest/index.json',
),
),
);
final items = dig<List>(index, []);
final identifiers = <Map<String, Object?>>[];
for (final item in items) {
identifiers.add({
'qualifiedName': dig(item, ['qualifiedName']),
'desc': 'Object holding options for retrying a function.',
});
}
results.add(
TextContent(
text: jsonEncode({
'packageName': packageName,
'latestVersion': latestVersion,
'description': description,
'scores': scores,
'topics': topics,
'licenses': licenses,
'api': identifiers,
}),
),
);
}

return CallToolResult(content: results);
} on Exception catch (e) {
return CallToolResult(
content: [TextContent(text: 'Failed searching pub.dev: $e')],
isError: true,
);
} finally {
client.close();
}
}

static final pubDevTool = Tool(
name: 'pub-dev-search',
description:
'Searches pub.dev for packages relevant to a given search query. '
'The response will describe each result with its download count,'
' package description, topics, license, and a list of identifiers '
'in the public api',
annotations: ToolAnnotations(title: 'pub.dev search', readOnlyHint: false),
inputSchema: Schema.object(
properties: {
'search-query': Schema.string(
title: 'Search query',
description: 'The query to run against pub.dev package search',
),
},
required: ['search-query'],
),
);
}

/// The set of supported `dart pub` subcommands.
enum SupportedPubCommand {
// This is supported in a simplified form: `dart pub add <package-name>`.
// TODO(https://github.com/dart-lang/ai/issues/77): add support for adding
// dev dependencies.
add(requiresPackageName: true),

get,

// This is supported in a simplified form: `dart pub remove <package-name>`.
remove(requiresPackageName: true),

upgrade;

const SupportedPubCommand({this.requiresPackageName = false});

final bool requiresPackageName;

static SupportedPubCommand? fromName(String name) {
for (final command in SupportedPubCommand.values) {
if (command.name == name) {
return command;
}
}
return null;
}

static String get listAll {
return _writeCommandsAsList(SupportedPubCommand.values);
}

static String get listAllThatRequirePackageName {
return _writeCommandsAsList(
SupportedPubCommand.values.where((c) => c.requiresPackageName).toList(),
);
}

static String _writeCommandsAsList(List<SupportedPubCommand> commands) {
final buffer = StringBuffer();
for (var i = 0; i < commands.length; i++) {
final commandName = commands[i].name;
buffer.write('`$commandName`');
if (i < commands.length - 2) {
buffer.write(', ');
} else if (i == commands.length - 2) {
buffer.write(' and ');
}
}
return buffer.toString();
}
}

/// Utility for indexing json data structures.
///
/// Each element of [path] should be a `String`, `int` or `(String, String)`.
///
/// For each element `key` of [path], recurse into [json].
///
/// If the `key` is a String, the next json structure should be a Map, and have
/// `key` as a property. Recurse into that property.
///
/// If `key` is an `int`, the next json structure must be a List, with that
/// index. Recurse into that index.
///
/// If `key` in a `(String k, String v)` the next json structure must be a List
/// of maps, one of them having the property `k` with value `v`, recurse into
/// that map.
///
/// If at some point the types don't match throw a [FormatException].
///
/// Returns the result as a [T].
T dig<T>(dynamic json, List<dynamic> path) {
var i = 0;
String currentPath() => path.take(i).map((i) => '[$i]').join('');
for (; i < path.length; i++) {
outer:
switch (path[i]) {
case final String key:
if (json is! Map) {
throw FormatException('Expected a map at ${currentPath()}');
}
json = json[key];
case final int key:
if (json is! List) {
throw FormatException('Expected a map at ${currentPath()}');
}
json = json[key];
case (final String key, final String value):
if (json is! List) {
throw FormatException('Expected a map at ${currentPath()}');
}
final t = json;
for (var j = 0; j < t.length; j++) {
final element = t[j];
if (element is! Map) {
throw FormatException('Expected a map at ${currentPath()}[$j]');
}
if (element[key] == value) {
json = element;
break outer;
}
}
case final key:
throw ArgumentError('Bad key $key in', 'path');
}
}

if (json is! T) throw FormatException('Unexpected value at $currentPath()');
return json;
}
2 changes: 2 additions & 0 deletions pkgs/dart_tooling_mcp_server/lib/src/server.dart
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import 'mixins/analyzer.dart';
import 'mixins/dart_cli.dart';
import 'mixins/dtd.dart';
import 'mixins/pub.dart';
import 'mixins/pub_dev_search.dart';
import 'utils/process_manager.dart';

/// An MCP server for Dart and Flutter tooling.
Expand All @@ -24,6 +25,7 @@ final class DartToolingMCPServer extends MCPServer
DartAnalyzerSupport,
DartCliSupport,
PubSupport,
PubDevSupport,
DartToolingDaemonSupport
implements ProcessManagerSupport {
DartToolingMCPServer({
Expand Down
1 change: 1 addition & 0 deletions pkgs/dart_tooling_mcp_server/pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ dependencies:
dds_service_extensions: ^2.0.1
devtools_shared: ^11.2.0
dtd: ^2.4.0
http: ^1.3.0
json_rpc_2: ^3.0.3
# TODO: Get this another way.
language_server_protocol:
Expand Down