Skip to content
TheChatArchiveThe Chat Archive
Claudeclaude-sonnet-4-20250514-thinking-32k· AnthropicHugging Face LMArena Human Preference 140K

i have an gradio frontend for my local llm. How to inculde MCP User. e.g. for file…

From a Chatbot Arena battleThe same person sent this prompt to two anonymous models at once. Compare it with Claudeclaude-3-7-sonnet-20250219’s answer.
  1. User

    i have an gradio frontend for my local llm. How to inculde MCP User. e.g. for file access. Give me an conrete roadmap with an conrete example. I am totally new to mcp

  2. Claude Assistant

    I'll help you integrate MCP (Model Context Protocol) with your Gradio frontend to add file access capabilities. Here's a concrete roadmap:

    What is MCP?

    MCP allows your LLM to securely access external resources like files, databases, and APIs through standardized servers.

    Roadmap

    Step 1: Install MCP Dependencies

    pip install mcp anthropic-mcp gradio
    

    Step 2: Set up a File System MCP Server

    Create mcp_server.py:

    import asyncio
    from mcp import Server
    from mcp.server.models import InitializationOptions
    from mcp.server.stdio import stdio_server
    from mcp.types import Resource, Tool, TextContent
    import os
    import json
    
    app = Server("file-server")
    
    @app.list_resources()
    async def handle_list_resources() -> list[Resource]:
        """List available files in the working directory"""
        resources = []
        for file in os.listdir('.'):
            if os.path.isfile(file):
                resources.append(Resource(
                    uri=f"file://{file}",
                    name=file,
                    description=f"File: {file}",
                    mimeType="text/plain"
                ))
        return resources
    
    @app.read_resource()
    async def handle_read_resource(uri: str) -> str:
        """Read file content"""
        if uri.startswith("file://"):
            filepath = uri[7:]  # Remove "file://" prefix
            with open(filepath, 'r') as f:
                return f.read()
        raise ValueError(f"Unsupported URI: {uri}")
    
    @app.list_tools()
    async def handle_list_tools() -> list[Tool]:
        """List available tools"""
        return [
            Tool(
                name="write_file",
                description="Write content to a file",
                inputSchema={
                    "type": "object",
                    "properties": {
                        "filename": {"type": "string"},
                        "content": {"type": "string"}
                    },
                    "required": ["filename", "content"]
                }
            )
        ]
    
    @app.call_tool()
    async def handle_call_tool(name: str, arguments: dict) -> list[TextContent]:
        """Handle tool calls"""
        if name == "write_file":
            filename = arguments["filename"]
            content = arguments["content"]
            with open(filename, 'w') as f:
                f.write(content)
            return [TextContent(type="text", text=f"Successfully wrote to {filename}")]
        raise ValueError(f"Unknown tool: {name}")
    
    async def main():
        async with stdio_server() as (read_stream, write_stream):
            await app.run(read_stream, write_stream, InitializationOptions())
    
    if __name__ == "__main__":
        asyncio.run(main())
    

    Step 3: Create MCP Client Integration

    Create mcp_client.py:

    import asyncio
    import json
    from mcp import ClientSession
    from mcp.client.stdio import stdio_client
    
    class MCPFileClient:
        def __init__(self):
            self.session = None
            
        async def connect(self):
            """Connect to the MCP server"""
            self.stdio_client = await stdio_client(["python", "mcp_server.py"])
            self.session = ClientSession(self.stdio_client[0], self.stdio_client[1])
            await self.session.initialize()
            
        async def list_files(self):
            """List available files"""
            if not self.session:
                await self.connect()
            resources = await self.session.list_resources()
            return [r.name for r in resources]
        
        async def read_file(self, filename):
            """Read file content"""
            if not self.session:
                await self.connect()
            content = await self.session.read_resource(f"file://{filename}")
            return content
        
        async def write_file(self, filename, content):
            """Write file content"""
            if not self.session:
                await self.connect()
            result = await self.session.call_tool("write_file", {
                "filename": filename,
                "content": content
            })
            return result[0].text
        
        async def close(self):
            """Close the connection"""
            if self.session:
                await self.session.close()
    

    Step 4: Integrate with Your Gradio Frontend

    Modify your existing Gradio app (app.py):

    import gradio as gr
    import asyncio
    from mcp_client import MCPFileClient
    import threading
    
    # Your existing LLM setup
    def your_llm_function(prompt, context=""):
        # Your existing LLM inference code here
        # Now you can include file context in the prompt
        full_prompt = f"Context: {context}\n\nUser: {prompt}"
        # Return your LLM response
        return "Your LLM response here"
    
    # MCP integration
    mcp_client = MCPFileClient()
    
    async def get_file_context(filenames):
        """Get content from selected files"""
        if not filenames:
            return ""
        
        context = ""
        for filename in filenames:
            try:
                content = await mcp_client.read_file(filename)
                context += f"\n--- Content of {filename} ---\n{content}\n"
            except Exception as e:
                context += f"\n--- Error reading {filename}: {str(e)} ---\n"
        return context
    
    async def list_available_files():
        """Get list of available files"""
        try:
            return await mcp_client.list_files()
        except Exception as e:
            return [f"Error: {str(e)}"]
    
    def chat_with_files(message, selected_files):
        """Chat function that includes file context"""
        # Get file context asynchronously
        loop = asyncio.new_event_loop()
        asyncio.set_event_loop(loop)
        context = loop.run_until_complete(get_file_context(selected_files))
        loop.close()
        
        # Generate response with context
        response = your_llm_function(message, context)
        return response
    
    def refresh_files():
        """Refresh the file list"""
        loop = asyncio.new_event_loop()
        asyncio.set_event_loop(loop)
        files = loop.run_until_complete(list_available_files())
        loop.close()
        return gr.update(choices=files)
    
    # Create Gradio interface
    with gr.Blocks() as demo:
        gr.Markdown("# LLM Chat with File Access (MCP)")
        
        with gr.Row():
            with gr.Column(scale=1):
                file_list = gr.CheckboxGroup(
                    label="Select Files for Context",
                    choices=[],
                    value=[]
                )
                refresh_btn = gr.Button("Refresh Files")
                
            with gr.Column(scale=3):
                chatbot = gr.Chatbot()
                msg = gr.Textbox(label="Message")
                send_btn = gr.Button("Send")
        
        # Event handlers
        refresh_btn.click(refresh_files, outputs=file_list)
        
        def respond(message, chat_history, selected_files):
            response = chat_with_files(message, selected_files)
            chat_history.append((message, response))
            return chat_history, ""
        
        send_btn.click(
            respond,
            inputs=[msg, chatbot, file_list],
            outputs=[chatbot, msg]
        )
        
        # Load files on startup
        demo.load(refresh_files, outputs=file_list)
    
    if __name__ == "__main__":
        demo.launch()
    

    Step 5: Run Your Application

    1. Start your Gradio app:
    python app.py
    
    1. The interface will show:
      • A file selection panel
      • Your chat interface
      • Files in your directory will be listed and selectable

    Step 6: Test the Integration

    1. Create a test file: echo "This is a test document about AI." > test.txt
    2. Refresh the file list in Gradio
    3. Select test.txt
    4. Ask your LLM: "What does the test file contain?"

    Next Steps

    • Add more MCP servers (database, web search, etc.)
    • Implement file upload capabilities
    • Add error handling and logging
    • Secure file access with permissions

    This setup gives you a working MCP integration that your LLM can use to read and write files through the Gradio interface. The MCP server runs as a separate process and communicates with your Gradio app through the MCP protocol.

Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.