MCPcopy Create free account
hub / github.com/modelcontextprotocol/go-sdk / readResource

Method readResource

mcp/server.go:791–818  ·  view source on GitHub ↗
(ctx context.Context, req *ReadResourceRequest)

Source from the content-addressed store, hash-verified

789}
790
791func (s *Server) readResource(ctx context.Context, req *ReadResourceRequest) (*ReadResourceResult, error) {
792 uri := req.Params.URI
793 // Look up the resource URI in the lists of resources and resource templates.
794 // This is a security check as well as an information lookup.
795 handler, mimeType, ok := s.lookupResourceHandler(uri)
796 if !ok {
797 // Don't expose the server configuration to the client.
798 // Treat an unregistered resource the same as a registered one that couldn't be found.
799 return nil, ResourceNotFoundError(uri)
800 }
801 res, err := handler(ctx, req)
802 if err != nil {
803 return nil, err
804 }
805 if res == nil || res.Contents == nil {
806 return nil, fmt.Errorf("reading resource %s: read handler returned nil information", uri)
807 }
808 // As a convenience, populate some fields.
809 for _, c := range res.Contents {
810 if c.URI == "" {
811 c.URI = uri
812 }
813 if c.MIMEType == "" {
814 c.MIMEType = mimeType
815 }
816 }
817 return res, nil
818}
819
820// lookupResourceHandler returns the resource handler and MIME type for the resource or
821// resource template matching uri. If none, the last return value is false.

Callers

nothing calls this directly

Calls 3

lookupResourceHandlerMethod · 0.95
ResourceNotFoundErrorFunction · 0.85
handlerInterface · 0.85

Tested by

no test coverage detected