liveBook · Manning

MCP resource

As explained in Spring AI in Action

An MCP resource is a named, described, URI-addressable source of contextual content exposed through the Model Context Protocol.

An MCP resource is a named, described, URI-addressable source of contextual content exposed by an MCP server. It can provide textual or binary data related to the server’s domain, allowing an application to supply structured context alongside user-generated prompts. section 7.5

Spring AI supports two implementation styles:

  • Declare a resource specification in a @Bean method, describing its URI, name, description, MIME type, annotations, and read behavior. section 7.5.1
  • Build the resource content from domain data, such as repository results, then return it through a ReadResourceResult. section 7.5.1
  • Use an annotated service method with @McpResource when the community annotation project’s simpler declaration style is appropriate. section 7.5.2

Game List resource

A resource at games://game-list can query the game repository, format each title as a line of text, and expose the result with the name “Game List” and MIME type text/plain. MCP Inspector can list and display the returned resource. section 7.5.1

Listing 7.6 Exposing a resource using Spring configuration
@Bean
public List<McpServerFeatures.SyncResourceSpecification>
    gameResources(GameRepository gameRepository) {
  List<McpSchema.Role> audience = List.of(McpSchema.Role.USER);
  McpSchema.Annotations annotations =
      new McpSchema.Annotations(audience, 1.0);

  var gameListResource = new McpSchema.Resource(    #1
      "games://game-list",
      "Game List",
      "A list of games available in the repository",
      "text/plain",
      annotations
  );

  var gameTitles = gameRepository.findAllTitles();  #2
  var gameListText = new StringBuilder();
  for (String title : gameTitles) {
    gameListText.append("- ").append(title).append("\n");
  }

  var gameListResourceSpec = 
    new McpServerFeatures.SyncResourceSpecification(  #3
      gameListResource, (exchange, request) -> {
    return new McpSchema.ReadResourceResult(
        List.of(new McpSchema.TextResourceContents(
            request.uri(),
            "text/plain",
            gameListText.toString())));
  });

  return List.of(gameListResourceSpec);  #4
}

Inspect the complete Spring configuration pattern for defining a URI, metadata, repository-backed content, and a synchronous resource specification. section 7.5.1

How Spring AI in Action covers it

How deep it goes
Explains metadata, repository-backed content, synchronous reads, inspection, and annotation alternatives without detailing the protocol internals.
Examples
Uses a repository-backed game list with a games://game-list URI and displays it through MCP Inspector.
What it assumes
Assumes a Spring MCP server and domain repository, while demonstrating resource metadata, content construction, and declaration styles.
What you can do after
Build it from scratch as a Spring MCP resource backed by repository data or an annotated provider method.
How it connects
Connects resources with MCP servers, prompts, Spring configuration, repositories, annotations, and MCP Inspector.

Also explained in

Compare how 4 books explain MCP resource →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Spring AI in Action. The section links point to the parts of the book it draws on.