Skip to content

MCP Completion Issue #1065

Closed
Closed

Description

@nileshmalode11

Question

mcp = FastMCP("completion1",
              instructions="A server that generates code from user prompts and JSON context. For all validation code generation requests from developers")

# Global store for extracted values
repo_cache = []

@mcp.tool()
async def long_task(files: list[str], ctx: Context) -> str:
    """Process a list of files asynchronously.

    Args:
        files (list[str]): _description_
        ctx (Context): _description_

    Returns:
        str: _description_
    """
    global repo_cache
    repo_cache.clear()  # reset

    for i, file in enumerate(files):
        ctx.info(f"Processing {file}")
        with open(file, "r", encoding="utf-8") as f:
            data = f.read()

        # Just for illustration: suppose lines in file are repo names
        lines = data.splitlines()
        repo_cache.extend(lines)

        await ctx.report_progress(i + 1, len(files))

    ctx.info(f"Cached repos: {repo_cache}")
    return "Processing complete"


@mcp.completion()
async def free_text_completion(argument: CompletionArgument) -> Completion:
    """Handle free text completion requests.
    Args:
        argument (CompletionArgument): The argument containing the user's input.
    Returns:
        Completion: A completion object containing suggestions based on the user's input.
    """    
    # Use the global repo_cache to provide suggestions
    global repo_cache
    suggestions = repo_cache or ["code_genrator", "some_other_repo"]
    matches = [r for r in suggestions if r.startswith(argument.value)]
    return Completion(values=matches)


if __name__ == "__main__":
    mcp.run(transport="stdio")

we have three queries as follows-
I have implemented a context loader & a completion endpoint as above I'm using mcp tools & it's completion endpoint with GitHub copilot in VS code.

Hear are my queries-

  1. How can I get my recommendation using completion endpoint in completion1.
  2. Where I will be getting the recommendation in VS code file editor or in GitHub copilot while MCP is connected & is running.
  3. What are the additional implementations I can add here to make the MCP'c workflow better.
    My mcp for now is loading the context & further generation are happening through it one I have sent a query from GitHub copilot.

Please help with the queries I have written above.

Activity

  1. Amitt1412 commented on Jul 1, 2025

    @Amitt1412

    I too have faced the same issues when I was working with the MCPs implementation for my environment. I struggled finding a proper implementation of MCP completion with GitHub copilot in VS Code.

  2. akshada-pawar22 commented on Jul 1, 2025

    @akshada-pawar22

    I’m facing the same issue

  3. changed the title [-]MCP Completion Endpoint Issue[/-] [+]MCP Completion Issue[/+] on Jul 2, 2025
  4. added
    bugSomething isn't working
    ready for workEnough information for someone to start working on
    P1Significant bug affecting many users, highly requested feature
    and removed
    questionFurther information is requested
    on Oct 7, 2025
  5. maxisbey commented on Jul 24, 2026

    @maxisbey
    Contributor

    MCP completions apply to exactly two things — the arguments of a prompt and the parameters of a resource template — and the client sends completion/complete while its user is filling one of those in (see the Completions guide). A server that only registers a tool never receives that request, so free_text_completion is never called: tool arguments are chosen by the model, not through completions. To answer (1), give the handler something to complete — e.g. a prompt with a repo argument — and use the full (ref, argument, context) handler signature; the example below adapts your repo cache and drives it with the in-memory client (v2 API: FastMCP is now MCPServer). For (2), where and whether those suggestions appear is the client's UI decision (VS Code / Copilot), not something this SDK controls. For (3), to get the cached repo list in front of the model, expose it as a resource or return it from a tool — completions are for a human filling in a form.

    Example
    import anyio
    from mcp_types import Completion, CompletionArgument, CompletionContext, PromptReference, ResourceTemplateReference
    
    from mcp import Client
    from mcp.server import MCPServer
    
    mcp = MCPServer("completion1")
    repo_cache = ["code_generator", "some_other_repo"]
    
    
    @mcp.prompt()
    def generate(repo: str, request: str) -> str:
        """Generate validation code for a repo."""
        return f"Generate {request} for repo {repo}"
    
    
    @mcp.completion()
    async def suggest_repo(
        ref: PromptReference | ResourceTemplateReference,
        argument: CompletionArgument,
        context: CompletionContext | None,
    ) -> Completion | None:
        if isinstance(ref, PromptReference) and ref.name == "generate" and argument.name == "repo":
            return Completion(values=[r for r in repo_cache if r.startswith(argument.value)])
        return None
    
    
    async def main() -> None:
        async with Client(mcp) as client:
            result = await client.complete(
                ref=PromptReference(name="generate"),
                argument={"name": "repo", "value": "code"},
            )
            print(result.completion.values)  # ['code_generator']
    
    
    anyio.run(main)

    Closing this as part of a wider backlog cleanup around the v2 release. Feel free to reopen if this is still relevant.

    AI Disclaimer

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    P1Significant bug affecting many users, highly requested featurebugSomething isn't workingready for workEnough information for someone to start working on

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions