77 "strings"
88 "time"
99
10+ "github.com/MegaGrindStone/go-mcp"
1011 "github.com/yuin/goldmark"
1112 highlighting "github.com/yuin/goldmark-highlighting"
1213 "github.com/yuin/goldmark/extension"
@@ -37,6 +38,9 @@ type Content struct {
3738 // Text would be filled if Type is ContentTypeText.
3839 Text string
3940
41+ // ResourceContents would be filled if Type is ContentTypeResource.
42+ ResourceContents []mcp.ResourceContents
43+
4044 // ToolName would be filled if Type is ContentTypeCallTool.
4145 ToolName string
4246 // ToolInput would be filled if Type is ContentTypeCallTool.
@@ -59,20 +63,31 @@ type Role string
5963type ContentType string
6064
6165const (
62- // RoleUser represents a user message. A message with this role would only contain text content.
66+ // RoleUser represents a user message. A message with this role would only contain text or resource content.
6367 RoleUser Role = "user"
64- // RoleAssistant represents an assistant message. A message with this role would contain text content
65- // and potentially other types of content.
68+ // RoleAssistant represents an assistant message. A message with this role would contain
69+ // all types of content but resource .
6670 RoleAssistant Role = "assistant"
6771
6872 // ContentTypeText represents text content.
6973 ContentTypeText ContentType = "text"
74+ // ContentTypeResource represents a resource content.
75+ ContentTypeResource ContentType = "resource"
7076 // ContentTypeCallTool represents a call to a tool.
7177 ContentTypeCallTool ContentType = "call_tool"
7278 // ContentTypeToolResult represents the result of a tool call.
7379 ContentTypeToolResult ContentType = "tool_result"
7480)
7581
82+ var mimeTypeToLanguage = map [string ]string {
83+ "text/x-go" : "go" ,
84+ "text/golang" : "go" ,
85+ "application/json" : "json" ,
86+ "text/javascript" : "javascript" ,
87+ "text/html" : "html" ,
88+ "text/css" : "css" ,
89+ }
90+
7691// RenderContents renders contents into a markdown string.
7792func RenderContents (contents []Content ) (string , error ) {
7893 var sb strings.Builder
@@ -83,6 +98,41 @@ func RenderContents(contents []Content) (string, error) {
8398 continue
8499 }
85100 sb .WriteString (content .Text )
101+ case ContentTypeResource :
102+ if len (content .ResourceContents ) == 0 {
103+ continue
104+ }
105+ for _ , resource := range content .ResourceContents {
106+ sb .WriteString (" \n \n <details>\n " )
107+ sb .WriteString (fmt .Sprintf ("<summary>Resource: %s</summary>\n \n " , resource .URI ))
108+
109+ if resource .MimeType != "" {
110+ sb .WriteString (fmt .Sprintf ("MIME Type: `%s`\n \n " , resource .MimeType ))
111+ }
112+
113+ if resource .Text != "" {
114+ // Use map for language determination
115+ language := "text"
116+ if lang , exists := mimeTypeToLanguage [resource .MimeType ]; exists {
117+ language = lang
118+ }
119+
120+ sb .WriteString (fmt .Sprintf ("```%s\n %s\n ```\n " , language , resource .Text ))
121+ } else if resource .Blob != "" {
122+ // Handle binary content
123+ if strings .HasPrefix (resource .MimeType , "image/" ) {
124+ // Display images inline
125+ sb .WriteString (fmt .Sprintf ("<img src=\" data:%s;base64,%s\" alt=\" %s\" />\n " ,
126+ resource .MimeType , resource .Blob , resource .URI ))
127+ } else {
128+ // Provide download link for other binary content
129+ sb .WriteString (fmt .Sprintf ("<a href=\" data:%s;base64,%s\" download=\" %s\" >Download %s</a>\n " ,
130+ resource .MimeType , resource .Blob , resource .URI , resource .URI ))
131+ }
132+ }
133+
134+ sb .WriteString ("\n </details> \n \n " )
135+ }
86136 case ContentTypeCallTool :
87137 sb .WriteString (" \n \n <details>\n " )
88138 sb .WriteString (fmt .Sprintf ("<summary>Calling Tool: %s</summary>\n \n " , content .ToolName ))
0 commit comments