modelcontextprotocol / modelcontextprotocol/typescript-sdk

[v2] Typescript: optional content when structuredContent is present

Aperta
#2,755 0 commenti 0 reazioni 0 assegnatari Vedi su GitHub

Nessuno ha ancora preso questa issue.

v2
Lingua principale
TypeScript
Stelle
13.4k
Fork
2.2k
Merge medio
3g 15h
PR unite (30g)
4

Descrizione

What happened?

If I create a tool with a structuredContent but no content, I get a typescript error saying that the content field is mandatory. But this is stupid for two reasons:

  1. For now at least, it seems like the SDK will anyway add a content automatically if not present… So let me save time (and redundancy) by avoiding to explicitly add it myself.

  2. Worst, according to the spec:

    For backwards compatibility, a tool that returns structured content SHOULD also return the serialized JSON in a TextContent block.

    The verb SHOULD is used, and not MUST, which means that we don't strictly need to follow this rule if we don't want to be backward compatible.

What did you expect?

I'd expect typescript to allow tools to return no content but only a structuredContent. Moreover, we should add an option to say if the SDK should add itself a serialization of the structuredContent into content as this is done for now: I believe that it is a good thing to enable it by default as it is done now, but for efficiency reasons it would be great to disable content and only keep structuredContent (at least in v2) since this is allowed by the specification.

Code to reproduce
server.registerTool(
    'ping',
    {
      description: 'Returns a string pong with a timestamp',
      outputSchema: z.string()
    },
    async () => {
      const str = `Pong at ${new Date()}`
      return {
        // content: [{ type: 'text', text: str }], // Error without this line
        structuredContent: str
      };
    }
  );
SDK version

2.0.0

Area

Server

Guida per i contributori

Apri la guida per i contributori

Come iniziare

  1. Leggi tutta la issue e poi la guida ai contributi del progetto.
  2. Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
  3. Fai un fork del repository e lavora su un branch.
  4. Apri una pull request che faccia riferimento al numero della issue.

Direzione di ricerca

Inizia dal punto di ingresso server.registerTool e dai tipi TypeScript per i risultati degli strumenti, quindi segui il modo in cui structuredContent viene serializzato in content. Usa la riproduzione di ping fornita per verificare che i risultati contenenti solo structuredContent superino il controllo dei tipi e che il comportamento di serializzazione possa essere controllato come richiesto.

Scritto dal modello di indicizzazione a partire dal testo della issue.

Valutazione

Stack tecnologico
typescript
Ambito
api, backend
Tipo di issue
Bug
Difficoltà
3/5
Tempo stimato
1-2 giorni
Stato di attività
Attiva
Chiarezza
Abbastanza chiara
Idoneità per principianti
62/100

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.