DEV Community

Cover image for Stop Using RAG for Small Document Sets! Build an MCP Resource Server with Spring AI
Ajeet Singh
Ajeet Singh

Posted on • Originally published at javaninja.io

Stop Using RAG for Small Document Sets! Build an MCP Resource Server with Spring AI

I recently built a real-time document server using Spring AI and the Model Context Protocol, and it completely changed how I think about giving AI access to documents.

TL;DR: If you have fewer than 100 documents that change frequently, skip the complexity of RAG and use MCP Resources instead. I'll show you exactly how.

🎬 Watch the Full Video Tutorial

Prefer reading? Keep scrolling! But the video includes live demos and troubleshooting tips.

Why I Stopped Defaulting to RAG

Like many developers, I used to reach for RAG (Retrieval Augmented Generation) for every document-based AI project:

  • Vector database? ✅ Check
  • Embedding model? ✅ Check
  • Chunking strategy? ✅ Check
  • Hours of setup? ✅ Ugh...

But then I hit a wall with a personal project: I just wanted AI to read 10 text files that I update daily.

Setting up Pinecone, generating embeddings, and dealing with re-indexing delays felt like using a sledgehammer to crack a nut.

That's when I discovered MCP Resources.

What is MCP? (In 60 Seconds)

Model Context Protocol (MCP) is an open standard by Anthropic that lets AI applications connect to data sources - think "USB ports for AI."

Instead of this (RAG):

Document → Chunk → Embed → Vector DB → Semantic Search → AI
Enter fullscreen mode Exit fullscreen mode

You get this (MCP Resources):

Document → AI (that's it!)
Enter fullscreen mode Exit fullscreen mode

The kicker? For my use case (personal documents, config files, live logs), MCP was 10x simpler.

What We're Building Today

In my video tutorial, I walk through building a production-ready MCP server that:

✅ Exposes documents from any directory as MCP resources

✅ Automatically detects file changes (add/modify/delete)

✅ Notifies connected clients in real-time

✅ Works with Spring AI out of the box

✅ Takes less than 30 minutes to implement

The Code (Simplified)

Here's the core of what we build in the video:

1. Main Application

@SpringBootApplication
public class SpringAiMcpServerResourcesApplication {

    @Value("${resource.directory.path}")
    private String directoryPath;

    public static void main(String[] args) {
        SpringApplication.run(SpringAiMcpServerResourcesApplication.class, args);
    }

    @Bean
    public List<McpServerFeatures.SyncResourceSpecification> myResources() {
        String path = directoryPath;
        Path folderPath = Paths.get(path);

        try(Stream<Path> paths = Files.list(folderPath)) {
            List<McpServerFeatures.SyncResourceSpecification> resources = paths
                    .map(this::getResourceSpecification)
                    .collect(Collectors.toList());

            return resources;
        } catch (IOException e) {
            return Collections.emptyList();
        }
    }

    private McpServerFeatures.SyncResourceSpecification getResourceSpecification(Path filePath) {
        String fileName = filePath.getFileName().toString();
        Path absolutePath = filePath.toAbsolutePath();
        String fileUri = "file://" + absolutePath;
        String mimeType = "text/plain";

        var documentResource = new McpSchema.Resource(
                fileUri,
                fileName,
                "Document: " + fileName,
                mimeType,
                null
        );

        McpServerFeatures.SyncResourceSpecification spec = new McpServerFeatures.SyncResourceSpecification(documentResource,
                (exchange, request) -> {
                    try {
                        String textContent = Files.readString(absolutePath);
                        McpSchema.TextResourceContents textContents = new McpSchema.TextResourceContents(request.uri(), mimeType, textContent);

                        return new McpSchema.ReadResourceResult(List.of(textContents));
                    } catch (IOException e) {
                        throw new RuntimeException(e);
                    }
                });
        return spec;
    }
}
Enter fullscreen mode Exit fullscreen mode

What's happening:

  • The @Bean scans your documents directory
  • Creates a ResourceSpecification for each file
  • Spring AI MCP automatically exposes them to clients

Use RAG when:

  • You have 1000+ documents
  • Documents are relatively static
  • You need semantic search ("find similar concepts")
  • Building a company knowledge base

Use MCP Resources when:

  • You have 1-100 documents
  • Documents change frequently
  • You need real-time access
  • Working with config files, logs, or personal docs
  • You want simple infrastructure

Key Takeaways

✅ RAG isn't always the answer - question your defaults

✅ MCP Resources are simpler for small document sets

✅ Real-time updates beat re-indexing delays

✅ 30-minute implementation vs hours of RAG setup

✅ Perfect for personal tools and small teams

What's Next?

Part 2 (Coming Soon): Building the MCP Client

  • Connecting to our server
  • Using AI to analyze documents
  • Automatic updates when files change

🔔 Subscribe on YouTube so you don't miss it!

Resources

📺 Full Video Tutorial - Watch the complete implementation

💻 Source Code on GitHub - Clone and run locally

🔗 MCP Specification - Protocol details

Follow me for more Spring AI tutorials! 🚀

Top comments (0)