-
Notifications
You must be signed in to change notification settings - Fork 22
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
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 65de883
Update pkgs/dart_tooling_mcp_server/lib/src/mixins/pub_dev_search.dart
sigurdm dbcabff
Update pkgs/dart_tooling_mcp_server/lib/src/mixins/pub_dev_search.dart
sigurdm ff7cc43
Detailed search term description
sigurdm a383fb5
Rename argument `search-query` -> `query`
sigurdm 0fe6f8c
Remove unused import
sigurdm 5426861
Underscores in tool name
sigurdm 245d790
Readonlyhint: true
sigurdm 8f5e9cb
Remove copy-paste waste
sigurdm ce713a1
Extract `dig` json utility
sigurdm cd3ba90
Remove unused query parameters
sigurdm f9219b4
Make client mockable
sigurdm f1f9f2c
Add test
sigurdm 2b28b8d
Better golden file error message
sigurdm 084cfe7
Privatify name
sigurdm c803a7b
Test failure
sigurdm e0afe2d
Run subqueries in parallel
sigurdm 29888c8
Test and fix json dig
sigurdm 7442c9c
Make pool top-level static
sigurdm 9070d8a
Update pkgs/dart_tooling_mcp_server/test_fixtures/pub_dev_responses/R…
sigurdm b46121c
Update pkgs/dart_tooling_mcp_server/test_fixtures/pub_dev_responses/R…
sigurdm 9e1283a
Update pkgs/dart_tooling_mcp_server/lib/src/utils/json.dart
sigurdm ab08279
json util, handle specified types
sigurdm 77ccf09
Update pkgs/dart_tooling_mcp_server/lib/src/mixins/pub_dev_search.dart
sigurdm 95da176
Handle generic types
sigurdm 7a4c89c
Remove nonsense
sigurdm 525ea44
Bump subosito/flutter-action from 1 to 2 in the github-actions group …
dependabot[bot] 2093225
Making `instructions` optional. (#98)
domesticmouse b10e03c
Require roots for all CLI tools (#101)
jakemac53 29cd5b2
Add runtime errors resource and tool to clear errors. (#94)
jakemac53 adf7ee9
add option to log protocol messages to a Sink<String> (#102)
jakemac53 fed9134
Modify DTD connection verbiage in example client (#104)
kenzieschmoll 4871a32
add test for server closing early to validate behavior (#105)
jakemac53 06c71c7
Continue making `instructions` optional. (#107)
domesticmouse 23aa993
Release dart_mcp version 0.2.0 (#106)
jakemac53 009219c
Add supported tools to the MCP server README (#109)
kenzieschmoll 346d7c4
add signature_help tool (#110)
jakemac53 f5c83bd
show thinking text and input/output token usage (#108)
jakemac53 cc41bfb
Tolerate missing sub-responses
sigurdm e483a8c
Report error when no packages found
sigurdm 472d00b
merge
sigurdm b30e7f9
dartfmt
sigurdm 10c723d
Extract result count constant
sigurdm 1f06c85
End with period
sigurdm 0745135
Use named records
sigurdm 152457d
End with period
sigurdm 524fac2
Break up lines
sigurdm b9d78aa
Extract test helper
sigurdm c3b41d8
Explain no 'or' operator in tool description
sigurdm d36f0f5
Merge
sigurdm 5636292
Fix merge
sigurdm d0cdb29
Sort dependencies
sigurdm 1e4524e
Address some of review
sigurdm e7f3935
Fix rename
sigurdm e3e187e
Refactor to use `runWithClient`
sigurdm 9e6e34b
dynamic -> Object
sigurdm b808801
Use list of strings instead of list of objects
sigurdm 6c98900
Restrict max number of listed identifiers
sigurdm a9c13b9
Report the publisher of the package
sigurdm File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
265 changes: 265 additions & 0 deletions
265
pkgs/dart_tooling_mcp_server/lib/src/mixins/pub_dev_search.dart
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 { | ||
sigurdm marked this conversation as resolved.
Show resolved
Hide resolved
|
||
@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?; | ||
sigurdm marked this conversation as resolved.
Show resolved
Hide resolved
|
||
if (query == null) { | ||
return CallToolResult( | ||
content: [ | ||
TextContent(text: 'Missing required argument `search-query`.'), | ||
sigurdm marked this conversation as resolved.
Show resolved
Hide resolved
|
||
], | ||
isError: true, | ||
); | ||
} | ||
final client = Client(); | ||
final searchUrl = Uri( | ||
scheme: 'https', | ||
host: 'pub.dev', | ||
path: 'api/search', | ||
queryParameters: {'q': query}, | ||
); | ||
sigurdm marked this conversation as resolved.
Show resolved
Hide resolved
|
||
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', | ||
sigurdm marked this conversation as resolved.
Show resolved
Hide resolved
|
||
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', | ||
sigurdm marked this conversation as resolved.
Show resolved
Hide resolved
|
||
annotations: ToolAnnotations(title: 'pub.dev search', readOnlyHint: false), | ||
sigurdm marked this conversation as resolved.
Show resolved
Hide resolved
|
||
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(); | ||
} | ||
} | ||
|
||
sigurdm marked this conversation as resolved.
Show resolved
Hide resolved
|
||
/// 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) { | ||
sigurdm marked this conversation as resolved.
Show resolved
Hide resolved
|
||
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; | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.