modelcontextprotocol / modelcontextprotocol/java-sdk
Un-deprecate/add no-arg builders
Personne n'a encore pris cette issue.
- Langage dominant
- Java
- Étoiles
- 3.7k
- Forks
- 1.1k
- Merge moyen
- 1 j 15 h
- PR mergées (30 j)
- 9
Description
#928 deprecated no-arg builders for schema types, e.g. McpSchema.Resource
I'd like you to consider reversing that decision, and also to add no-arg versions for types without them (e.g. ProgressNotification).
I understand the motivation that builders not be left in an invalid state. It's worth noting that it might achieve that aim currently (I didn't audit all the classes), but if the schema ever has any fields which are mutually exclusive or conditionally required, then using constructors will only offer partial protection.
The problem is that it makes it undermines one of the main advantages of the builder pattern, which is to make construction clearer.
Here was my attempt to write a CreateMessageRequest with only required properties:
var request = McpSchema.CreateMessageRequest.builder(
List.of(McpSchema.SamplingMessage.builder(
McpSchema.Role.USER,
McpSchema.TextContent.builder("Test Sampling Message").build()).build()
),
50
)
.build();
Maybe there's a better way to format/indent this, but I tried several variations and I thought this was the best one.
Compare that with the same thing constructed with named properties
var request = McpSchema.CreateMessageRequest.builder()
.messages(List.of(
McpSchema.SamplingMessage.builder()
.role(McpSchema.Role.USER)
.content(McpSchema.TextContent.builder("Test Sampling Message").build())
.build()
))
.maxTokens(50)
.build();
It's also worth noting that some builders have APIs like ModelPreferences#addHint. If that pattern were applied consistently, it could be simplified a little more by replacing messages(List.of( with addMessage(
Beyond readability
I'm writing a framework and builders enforcing all required params at once makes them inflexible for some possible API designs.
For example, my framework automatically manages progress tokens. I considered a design such as
void sendProgress(Consumer<McpSchema.ProgressNotification.Builder> consumer);
// an example caller. mcp creates the builder and applies the token
mcp.sendProgress(p -> p.progress(5).total(10));
However, it's not possible for the framework to implement this with the current builder methods. The user of the framework knows the progress value, the framework itself knows the progress token, but the builder expects both at once.
So the builder has to be worked around, for example to this
void sendProgress(double progress, Consumer<McpSchema.ProgressNotification.Builder> consumer);
// an example caller
mcp.sendProgress(5, p -> p.total(10));
Guide de contribution
Ouvrir le guide de contribution
Par où commencer
- Lisez l'issue en entier, puis le guide de contribution du projet.
- Signalez en commentaire que vous la prenez — cela évite que deux personnes fassent le même travail.
- Forkez le dépôt et travaillez sur une branche.
- Ouvrez une pull request qui référence le numéro de l'issue.
Piste de recherche
Commencez par les points d’entrée du builder McpSchema nommés ici, notamment CreateMessageRequest, SamplingMessage, TextContent, ProgressNotification et ModelPreferences. Comparez les API avec arguments obligatoires et sans arguments, puis vérifiez si les valeurs du schéma peuvent être fournies progressivement, y compris les tokens de progression gérés par le framework ; le travail est terminé lorsque l’approche du builder est cohérente pour tous les types demandés.
Rédigé par le modèle d'indexation à partir du texte de l'issue.
Évaluation
- Stack technique
- java
- Domaine
- api, backend-api-design
- Type d'issue
- Fonctionnalité
- Difficulté
- 5/5
- Temps estimé
- Plus d'une semaine
- Activité
- Calme
- Clarté
- Plutôt claire
- Accessibilité débutants
- 42/100